【问题标题】:NSXMLParsing for particular tag value特定标签值的 NSXMLParsing
【发布时间】:2011-03-15 07:53:09
【问题描述】:

我有一个如下所示的 XML 文件:

 <country>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Larne</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Troon </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Larne</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Cairnryan </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Belfast</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Birkenhead </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Belfast</SourcePort>
                <DestinationCountry>Belgium</DestinationCountry>
                <DestinationPort>Heysham </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Belgium</SourceCountry>
                <SourcePort>Warrenpoint</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Heysham </DestinationPort>
                        </routes>

我想解析&lt;SourceCountry&gt; == Ireland and &lt;DestinationCountry&gt; == UK 的特定数据。我该怎么做?

【问题讨论】:

  • 请停止发布重复的问题。我已将两者合并到其中,因为重复的答案投票赞成。

标签: nsxmlparser


【解决方案1】:

使用 NSXMLParser

  1. 为这个类实现委托...&lt;NSXMLParserDelegate&gt;

  2. 实现这个解析器的方法...

    您将需要以下方法..

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

您可以阅读this tutorial;使用起来很简单。

【讨论】:

    【解决方案2】:
    【解决方案3】:

    你需要使用 XML 解析器,使用 NSXMLParser 来解析你的数据。 下面是苹果使用 NSXMLParser 解析 XML 内容的 ImageMap 示例代码。

    http://developer.apple.com/library/mac/#samplecode/ImageMap/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009015

    【讨论】:

      【解决方案4】:

      试试这个。 . .

      整数元素=0; [self parseXMLFileAtURL:filepath];

      以下方法是 NSXMLParser 的委托

      - (void)parseXMLFileAtURL:(NSString *)URL { NSURL *xmlURL = [NSURL fileURLWithPath:URL]; parser = [[ NSClassFromString(@"NSXMLParser") alloc] initWithContentsOfURL:xmlURL]; [解析器 setDelegate:self]; [解析器 setShouldProcessNamespaces:NO]; [解析器 setShouldReportNamespacePrefixes:NO]; [解析器 setShouldResolveExternalEntities:NO]; [解析器解析]; } - (void)parserDidStartDocument:(NSXMLParser *)parser { NSLog(@"找到文件并开始解析"); } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName 属性:(NSDictionary *)attributeDict { if(([elementName compare:@"SourceCountry"])==NSOrderedSame) 元素=1; if(([elementName compare:@"SourcePort"])==NSOrderedSame) 元素=2; if(([elementName compare:@"DestinationCountry"])==NSOrderedSame) 元素=3; if(([elementName compare:@"DestinationPort"])==NSOrderedSame) 元素=4; } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName { } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 如果(元素==1) { [TotalXMLArray addObject:string]; 元素=0; } 如果(元素==2) { [TotalXMLArray addObject:string]; 元素=0; } 如果(元素==3) { [TotalXMLArray addObject:string]; 元素=0; } 如果(元素==4) { [TotalXMLArray addObject:string]; 元素=0; } }

      【讨论】:

      • 如何比较“ ==Ireland 和 == UK”的字符串解析字符串
      • 当我在标记中找到字符串时,我存储在名为 TotalXMLArray 的数组中,并且我正在使用数组索引访问该值,而不是尝试将值存储在字典中,我的意思是存储 4字典中的标签并将该字典存储在数组中,您将获得包含 xml 数据的字典数组
      • cocoawithlove.com/2008/10/…,使用此链接,您不必费力地解析 xml,xpathquery 它自己会在单个方法调用中为您提供字典的解析数组。
      • thanx pradeep 我是 iphone 新手。我只想解析那些内容特定值的标签,例如“ Ireland 和 UK”
      • 即使我几个月前开始 iphone 编程,:-) 使用 xpathquery,点击该链接,您将找到如何集成 xpath,并访问 w3schools 网站以获取有关如何编写 xpathqueries 的更多信息,在您的care 查询将是“/country/routes/sourceCountry”和“/country/routes/destinationCountry”
      猜你喜欢
      • 1970-01-01
      • 2022-08-23
      • 2013-01-15
      • 2021-12-07
      • 2012-12-04
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      相关资源
      最近更新 更多