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开发者_JAVA百科:(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
This post on Big Nerd Ranch taught me a lot about using NSXMLParser to parse XML.
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.
精彩评论