我有一个xml文件,我可以使用NSXML解析器进行解析。现在我想开始从特定标签解析,如何做到这一点?
<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>
现在解析应该从State标签开始,但它解析整个文档并显示城市标签中的名称,时间和注释。从指定标签解析,如何做?
答案 0 :(得分:1)
看看:
- (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];