Iphone 如何从特定标记解析xml
我有一个xml文件,我可以使用NSXML解析器进行解析。现在我想从特定的标记开始解析,这是如何做到的Iphone 如何从特定标记解析xml,iphone,xml,parsing,Iphone,Xml,Parsing,我有一个xml文件,我可以使用NSXML解析器进行解析。现在我想从特定的标记开始解析,这是如何做到的 <RootElement> <City> <Name>WC</Name> <Time>6 am</Time> <Notes>Hi</Notes> </City> <State> <Name>ABC</Name> <Time>
<RootElement>
<City>
<Name>WC</Name>
<Time>6 am</Time>
<Notes>Hi</Notes>
</City>
<State>
<Name>ABC</Name>
<Time>6 a.m.</Time>
<Time>2 a.m.</Time>
<Notes>This is a note for ABC</Notes>
</State>
<State>
<Name>DEF</Name>
<Time>8 am</Time>
<Time>10 am</Time>
<Notes>This is a note for DEF</Notes>
</State>
</RootElement>
厕所
早上6点
你好
基础知识
早上6点。
凌晨2点。
这是ABC的一张便条
DEF
早上8点
上午10点
这是DEF的注释
现在解析应该从State标记开始,但它解析整个文档,并显示城市标记中的名称、时间和注释。从指定标记解析,如何完成?请看:
- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error
编辑:代码示例
NSError* error;
CXMLDocument *doc = [[CXMLDocument alloc] initWithData:Data options:0 error:&error];
// figure out what nodes are in the doc, and pick it apart with the nodesForXPath call
NSArray * results = [doc nodesForXPath:@"/RootElement" error:&error];
NSString * finalUrl = self.defaultURL;
if ( [results count] > 0 ) {
NSString * element = [(CXMLElement *)[results objectAtIndex:0] stringValue];
if ( [element isEqualToString:@"State") {
// Do state processing
}
}
[doc release];
有点困惑,我是一个新手,请告诉我如何在didStartElement中使用它?我添加了一些示例代码。也可以看看这篇文章: