简体   繁体   中英

XMLParser repeating elements on iphone

I have an NSXMLParser , which is parsing an XML file, using Apple's implementation of event-driven XML handling.

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:

<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). Do you guys have any idea why?

Thanks!

You should get your Element result in didEndElement

Here is example

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html

In - (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM