0

I have a simple XML file. I just want to get user name and password. But it's generated repeated value when i was use nslog to observe the result. XML structure is just like this:

<Login xmlns="http://tempuri.org/">
  <userName>Vincent</userName>
  <password>string</password>
  <status>OK</status>
</Login>



- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"userName"]) {
        NSLog(@"Node is found correctly");
    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (!userNameString) {
        userNameString=[[NSMutableString alloc]init];
    }
    [userNameString appendString:string];
    NSLog(@"%@",userNameString);
}


- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSInteger errorCode=[parseError code];
//    NSLog(@"%d,%@",errorCode,[parseError localizedDescription]);
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"userName"]) {
        userNameString=nil;
    }
}

Here is result which was generated by NSLog:

2012-05-02 19:47:27.620 DeliveryManagement[4364:11003]

  Vincent

2012-05-02 19:47:27.620 DeliveryManagement[4364:11003]

2012-05-02 19:47:27.621 DeliveryManagement[4364:11003] string 2012-05-02 19:47:27.622 DeliveryManagement[4364:11003] string

2012-05-02 19:47:27.623 DeliveryManagement[4364:11003] string OK 2012-05-02 19:47:27.623 DeliveryManagement[4364:11003] string OK

2012-05-02 19:47:27.624 DeliveryManagement[4364:11003] string OK

2012-05-02 19:47:27.625 DeliveryManagement[4364:11003] string OK

2012-05-02 19:47:27.626 DeliveryManagement[4364:11003] string OK

2012-05-02 19:47:27.626 DeliveryManagement[4364:11003] string OK

2012-05-02 19:47:27.627 DeliveryManagement[4364:11003] string OK

2012-05-02 19:47:27.628 DeliveryManagement[4364:11003] Node is found correctly 2012-05-02 19:47:27.642 DeliveryManagement[4364:11003] string OK

Vincent Xu
  • 1
  • 2
  • 3

1 Answers1

0
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"userName"]) {
    NSLog(@"Node is found correctly");
    if(userNameString == nil)
        userNameString = [NSMutableString string];
    else 
        [userNameString setString:@""];
}
else {
    userNameString = nil;
}

}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

[userNameString appendString:string];
if (userNameString) 
{
    userNameString = nil;
}
NSLog(@"%@",userNameString);
}

just add this code and it should work. basically repeated values are because of a '\n' character after every value. so we need to make userNameString = nil; after appending. this way it goes into

  • (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

but its nil so no value is appended. hope it helps you..

Anshuk Garg
  • 1,540
  • 12
  • 14