使用 Objective-C 进行 TBXML 解析
我正在尝试使用 TBXML 解析以下 xml。我想创建一个包含所有项目标签值的数组。如何遍历“item”标签?
<root>
<item>
<northeast_area>
<item>
<new_england_north xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[8]">
<item xsi:type="xsd:string">boston s, ma</item>
<item xsi:type="xsd:string">providence, ri</item>
<item xsi:type="xsd:string">boston, ma </item>
<item xsi:type="xsd:string">portland, me</item>
<item xsi:type="xsd:string">boston, ma </item>
<item xsi:type="xsd:string">boston central</item>
<item xsi:type="xsd:string">boston north, ma</item>
<item xsi:type="xsd:string">boston south, ma</item>
</new_england_north>
</item>
<item>
<upstate_new_york xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[6]">
<item xsi:type="xsd:string">binghampton, ny</item>
<item xsi:type="xsd:string">rochester, ny</item>
<item xsi:type="xsd:string">albany s, ny</item>
<item xsi:type="xsd:string">syracuse, ny</item>
<item xsi:type="xsd:string">albany, ny</item>
<item xsi:type="xsd:string">buffalo, ny</item>
</upstate_new_york>
</item>
</northeast_area>
</item>
Am trying to parse the below xml using TBXML. I want to create an array which contains all the item tag values. How can I traverse through the "item" tags?
<root>
<item>
<northeast_area>
<item>
<new_england_north xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[8]">
<item xsi:type="xsd:string">boston s, ma</item>
<item xsi:type="xsd:string">providence, ri</item>
<item xsi:type="xsd:string">boston, ma </item>
<item xsi:type="xsd:string">portland, me</item>
<item xsi:type="xsd:string">boston, ma </item>
<item xsi:type="xsd:string">boston central</item>
<item xsi:type="xsd:string">boston north, ma</item>
<item xsi:type="xsd:string">boston south, ma</item>
</new_england_north>
</item>
<item>
<upstate_new_york xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[6]">
<item xsi:type="xsd:string">binghampton, ny</item>
<item xsi:type="xsd:string">rochester, ny</item>
<item xsi:type="xsd:string">albany s, ny</item>
<item xsi:type="xsd:string">syracuse, ny</item>
<item xsi:type="xsd:string">albany, ny</item>
<item xsi:type="xsd:string">buffalo, ny</item>
</upstate_new_york>
</item>
</northeast_area>
</item>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是一段从文件读取 XML 的示例代码。您可以调整它以使用可用的 NSString/NSData 对象。 请注意,由于我们不知道您的 SOAP 调用可以返回的确切潜在格式,因此这不是很可靠,它只是一个适用于您提供的数据的解决方案。
输出:
使用它,创建一个
NSArray
并存储项目,或者创建一个NSArray
应该相当简单对于每个区域设置并以这种方式分隔项目。 (将它们存储在由区域设置键入的NSDictionary
中。)Here's a piece of sample code that reads the XML from a file. You can tweak it to use an available NSString/NSData object. Note that since we don't know the exact potential formats that your SOAP calls can return, this isn't very robust, it's just a solution that works for the data you've provided.
And the output:
Using this, it should be fairly straightforward to create an
NSArray
and store the items, or create anNSArray
for each locale and separate the items that way. (Storing them in anNSDictionary
keyed by the locale.)