使用 TBXML 并解析并不总是存在的属性
我正在使用 TBXML 解析 xml 文件。 我的xml是这样的:
<locations>
<location>
<id>1</id>
<name>hello</name>
</location>
<location>
...
</locations>
一切正常,但是有一个大问题:有时xml可以“跳过”“name”标签,所以,例如,有这样的东西:
...
</location>
<location>
<id>43</id>
</location>
<location>
...
问题出在哪里? 使用此代码
TBXMLElement *location = [TBXML childElementNamed:@"location" parentElement:root];
while (location){
TBXMLElement *id = [TBXML childElementNamed:@"id" parentElement:location];
TBXMLElement *name = [TBXML childElementNamed:@"name" parentElement:location];
... //do something
location = location -> nextSibling;
}
应用程序会崩溃读取标签“名称”,因为有时没有......
我该如何解决它?
谢谢!
I'm parsing an xml file using TBXML.
my xml is like this:
<locations>
<location>
<id>1</id>
<name>hello</name>
</location>
<location>
...
</locations>
all works fine, but there is a big problem: sometimes the xml can "skip" the "name" tag, so, for example, there is something like this:
...
</location>
<location>
<id>43</id>
</location>
<location>
...
where is the problem?
that using this code
TBXMLElement *location = [TBXML childElementNamed:@"location" parentElement:root];
while (location){
TBXMLElement *id = [TBXML childElementNamed:@"id" parentElement:location];
TBXMLElement *name = [TBXML childElementNamed:@"name" parentElement:location];
... //do something
location = location -> nextSibling;
}
the app crashes reading the tag "name" because sometimes there isn't...
How can i solve it??
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该应用程序可能会崩溃,因为您的
... //do Something
假设name
为 none-nil。如果在 XML 中找不到
元素,则代码中的name
var 最终为零。您在“做某事”评论中剪掉的代码是什么?
顺便说一句,我通过谷歌搜索“tbxml 检查子节点是否存在”之类的内容找到了此信息,并找到以下页面:
http://www.tbxml.co.uk/forum/viewtopic.php?f=4&t=12
The app is probably crashing because your
... //do something
is assuming thatname
is none-nil. If the<name>
element wasn't found in the XML, thename
var in your code ends up being nil.What is the code that you snipped out in the "do something" comment?
Btw, I found this info by googling for something like "tbxml check for child node existence" and finding the following page:
http://www.tbxml.co.uk/forum/viewtopic.php?f=4&t=12