shaw kwok
shaw kwok

Reputation: 241

NSXMLParser - Parse an XML-file but it shows me the results twice

I'm trying to parse a XML-file to a NSMutableArray and show it to the tableview. The problem is when it parse, it add 2 times the results of the parsing in the NSMutableArray. This lead to the tableview showing the result twice. (2 rows in the tableview)

My question is : how can i show one result instead of the twice the same result?

XML-file:

 <something>test1234567test</something>

The code:

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

{   if([currentElement isEqualToString:@"something"])

    [currentname appendString:string];
    [mutarray_xml addObject:currentname];
}

i tried :

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
   if([currentElement isEqualToString:@"something"])
   {
      [currentname release];
   }
}

but it shows:

test1234567test

test1234567test

Upvotes: 1

Views: 246

Answers (2)

MusiGenesis
MusiGenesis

Reputation: 75296

To do parsing like this correctly, you also need to implement the didStartElement method. You should not be adding anything to your mutable array inside the foundCharacters method - you should only add something to the array in the didEndElement method, because this method indicates that the entire contents of the element has been read.

Upvotes: 0

nevan king
nevan king

Reputation: 113757

This post on Big Nerd Ranch taught me a lot about using NSXMLParser to parse XML.

Upvotes: 2

Related Questions