我正在尝试从 URL 解析一些 XML 数据。它几乎可以工作,但没有记录预期的数据。
这是xml数据的示例
<?xml version="1.0" encoding="utf-8"?> <result>
<row>
<id>1</id>
<menu_id>1</menu_id>
<group_id>1</group_id>
<name>Merchandise</name>
<image>bag of beans.jpg</image>
</row>
<row>
<id>4</id>
<menu_id>1</menu_id>
<group_id>1</group_id>
<name>Misc</name>
<image>tea.jpg</image>
</row>
这是我的代码:
#pragma mark - NSXMLParserDelegate
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
// If you have a more complex object you could init here
// Since I'm just storing a string I won't do anything
if ([elementName isEqualToString:@"name"]) {
// NSLog(@"didStartElement - %@", elementName);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!self->currentStringValue) {
self->currentStringValue = [[NSMutableString alloc] init];
// NSLog(@"didStartElement - %@", string);
}
[self->currentStringValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
NSString *currentStringValue = [self->currentStringValue
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
self->currentStringValue = nil;
if ([elementName isEqualToString:@"name"]) {
self->ourExpectedElementValue = currentStringValue;
NSLog(@"%@",currentStringValue);
}
}
正如您在 XML 数据中看到的那样,有多个“名称”元素和倒数第三行读取
NSLog(@"%@",currentStringValue);
旨在记录所有<name>
标签中的所有值,但它只记录以下内容:
2013-07-31 19:02:56.394 Touch2Go[1775:11303] Merchandise