I have a xml file, i can do the parsing using the NSXML parser. Now i want to start parsing from specific tag , how this can be done ?
我有一个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>
Now the parsing should start from State tag , but it parses entire document and displays the name,time,notes from the City Tag also. Parsing from specified tag , how it can be done ?
现在解析应该从State标签开始,但它解析整个文档并显示城市标签中的名称,时间和注释。从指定标签解析,如何做?
1 个解决方案
#1
1
Take a look at:
看一眼:
- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error
EDIT: Code example
编辑:代码示例
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];
#1
1
Take a look at:
看一眼:
- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error
EDIT: Code example
编辑:代码示例
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];