[英]XMLParser repeating elements on iphone
I have an NSXMLParser
, which is parsing an XML file, using Apple's implementation of event-driven XML handling. 我有一个NSXMLParser
,它使用Apple对事件驱动的XML处理的实现来解析XML文件。
I have it setup correctly, but when I get to the actual parsing, it goes over the same element twice as it parses through. 我已经正确设置了它,但是当我进行实际解析时,它在解析同一元素时会翻阅两次。
My code looks like this: 我的代码如下所示:
AppDelegate *myApp = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSData *tempData = [myApp.stringDetails dataUsingEncoding:NSUTF8StringEncoding];
NSString *textFields = [NSString string];
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:tempData];
[parser setDelegate: self];
if ([parser parse]!= YES) {
NSLog(@"%@", parser.parserError.description);
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
textFields = elementName;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([textFields isEqualToString:@"firstName"]) {
[firstName setText:string];
NSLog(@"first name is %@", string);
return;
}
if ([textFields isEqualToString:@"lastName"]) {
[lastName setText:string];
NSLog(@"last name is %@", string);
return;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
return;
}
The XML document i'm parsing looks like this: 我正在解析的XML文档如下所示:
<details>
<firstName>tester</firstName>
<lastName>testingson</lastName>
<address>
<street1>123 Main St.</street1>
<street2></street2>
<city>Anywhere</city>
<state>BC</state>
<country></country>
<postalCode>A0A0B0</postalCode>
</address>
<company>TEST</company>
<email>testing@test.com</email>
<cabbieNo>TEST</cabbieNo>
<driversLicense>TEST</driversLicense>
<plateNo>TEST</plateNo>
<services>
<taxi>1</taxi>
<ladies>1</ladies>
<limo>1</limo>
<handicap>1</handicap>
</services>
</details>
Strangely, my debug logs like this: 奇怪的是,我的调试日志如下:
2012-10-01 15:36:35.742 myAPP[9974:c07] first name is tester
2012-10-01 15:36:35.743 myAPP[9974:c07] first name is
2012-10-01 15:36:35.743 myAPP[9974:c07] last name is testingson
2012-10-01 15:36:35.744 myAPP9974:c07] last name is
As you can see, it looks through each element twice, but then on the second time, string
is empty (which shouldn't happen..in fact there shouldn't even be a second foundCharacters
event). 如您所见,它遍历每个元素两次,但是第二次, string
为空(这不应该发生。实际上,甚至不应该再有一个foundCharacters
事件)。 Do you guys have any idea why? 你们知道为什么吗?
Thanks! 谢谢!
You should get your Element result in didEndElement 您应该在didEndElement中获得Element结果
Here is example 这是例子
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html
In - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
在- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
You should append string(found characters) eg: 您应该附加字符串(找到的字符),例如:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!currentStringValue) {
// currentStringValue is an NSMutableString instance variable
currentStringValue = [[NSMutableString alloc] initWithCapacity:50];
}
[currentStringValue appendString:string];
}
Hope this will be helpful to you. 希望这对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.