我有一个像这样的XML节点:
<State id="1"> AA </State>
如何读取值'AA'?我可以使用:
读取id
值“1”
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict { <br>
//Extract the attribute here.
aState.stateId = [[attributeDict objectForKey:@"id"] integerValue];
NSLog(@"Reading id value :%i", aState.stateId);
}
NSLog(@"Processing Element: %@", elementName);
}
但无法读取值
答案 0 :(得分:0)
据我记忆,要做到这一点,你应该实现这个方法
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
整个代码如下所示:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
foundState = [elementName isEqualToString:@"State"];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (foundState) {
[stringBuffer appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if (foundState) {
NSLog(@"AA = %@", stringBuffer);
foundState = NO;
}
}