NSXML解析特定标签值
我有一个如下所示的 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>
我想解析
。我该怎么做?
I have an XML file that looks like this:
<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>
I want parse particular data for <SourceCountry> == Ireland and <DestinationCountry> == UK
. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用NSXMLParser
实现此类的委托...
实现此解析器的方法...
您将需要以下方法..
- (void)解析器:(NSXMLParser *)解析器 didStartElement:(NSString *)elementName 命名空间URI:(NSString *)namespaceURI 限定名称:(NSString *)限定名称属性:(NSDictionary *)attributeDict
< /p>- (void)parser:(NSXMLParser *)parserfoundCharacters:(NSString *)string
您可以阅读 本教程;使用起来很简单。
Use NSXMLParser
implement the delegate for this class ...
<NSXMLParserDelegate>
implement the methods for this parser...
YOU will need the following methods..
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
You can read this tutorial; it is simple to use.
这可能有助于
使用 NSXMLParser 解析 RSS 时访问特定子元素
This may help
Accessing specific child elements when parsing RSS with NSXMLParser
您需要使用 XML 解析器,使用 NSXMLParser 来解析您的数据。
下面是苹果的 ImageMap 示例代码,其中他们使用 NSXMLParser 来解析 XML 内容。
http:// developer.apple.com/library/mac/#samplecode/ImageMap/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009015
You need to use XML parser, Use NSXMLParser to parse you data.
Below is the apple sample code of ImageMap in which they have used the NSXMLParser to parse XML content.
http://developer.apple.com/library/mac/#samplecode/ImageMap/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009015
试试这个。 。 。
以下方法是 NSXMLParser 的委托
Try this . . .
following methods are the delegates of NSXMLParser