解析XML内部XML标签Objective-C [英] Parsing XML Inner XML tags Objective-C

查看:62
本文介绍了解析XML内部XML标签Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在以下XML中为每个工作站创建一个NSDictionary或(NSArray)的NSDictionary对象完整的对象.

I would like to create an NSDictionary or (NSArray) full of NSDictionary objects for each station in the following XML:

<stations lastUpdate="1328986911319" version="2.0">
    <station>
        <id>1</id>
        <name>River Street , Clerkenwell</name>
        <terminalName>001023</terminalName>
        <lat>51.52916347</lat>
        <long>-0.109970527</long>
        <installed>true</installed>
        <locked>false</locked>
        <installDate>1278947280000</installDate>
        <removalDate/>
        <temporary>false</temporary>
        <nbBikes>12</nbBikes>
        <nbEmptyDocks>7</nbEmptyDocks>
        <nbDocks>19</nbDocks>
    </station>

    ... more...

    <station>
        <id>260</id>
        <name>Broadwick Street, Soho</name>
        <terminalName>003489</terminalName>
        <lat>51.5136846</lat>
        <long>-0.135580879</long>
        <installed>true</installed>
        <locked>false</locked>
        <installDate>1279711020000</installDate>
        <removalDate/>
        <temporary>false</temporary>
        <nbBikes>12</nbBikes>
        <nbEmptyDocks>4</nbEmptyDocks>
        <nbDocks>18</nbDocks>
    </station>

    ...

 </stations>

实现此目标的最佳方法是什么?现在,我有一个NSDictionary,其中包含一个对象和一个键-"stations",但我想要NSDictionary(或NSArray)的NSDictionary.

What's the best way to achieve this? Right now I have an NSDictionary with one object and one key - "stations", but I want the NSDictionary (or NSArray) of NSDictionarys.

我正在使用Troy Brant的XML解析器-http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

I'm using an XML parser by Troy Brant - http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

我猜想它将涉及某种循环,但是我不确定如何解决这个问题.任何对正确方向的帮助或指导,将不胜感激.

I'm guessing it's going to involve some looping of some sort but I'm not really sure how to approach this problem. Any help or pointers in the right direction would be much appreciated.

谢谢.

推荐答案

我也使用XMLReader,这很容易理解
我查看了您的xml,并假设您想使用站点标签数组.
这是我的解决方案:

I also use the XMLReader it is very easy to understand
I have looked at your xml, and I am assuming you wanted to use the array of station tags.
Here is my solution:

NSDictionary *dictXML= [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
NSArray *arrStation = [[dictXML objectForKey:@"stations"] objectForKey:@"station"];//this would return the array of station dictionaries

现在,您有了工作站标签的数组,就可以执行所需的操作,例如显示所有id:

Now that you have the array of station tags you can do what you want for example displaying all id:

for(int i=0;i<[arrStation count];i++){
   NSDictionary *aStation = [arrStation objectAtIndex:i];
   NSLog(@"id = %@",[aStation objectForKey:@"id"]);
}

您还可以使用快速枚举循环编写更少的代码:

also you can write less code using the fast enumeration loop:

for(NSDictionary *aStation in arrStation){
  NSLog(@"id = %@",[aStation objectForKey:@"id"]);
}

希望有帮助:)

这篇关于解析XML内部XML标签Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆