NSXMLParser - 解析 XML 文件,但它向我显示结果两次
我正在尝试将 XML 文件解析为 NSMutableArray 并将其显示给表视图。问题是当它解析时,它会在 NSMutableArray 中添加 2 倍的解析结果。这导致表格视图两次显示结果。 (表格视图中的 2 行)
我的问题是:我如何显示一个结果而不是两次相同的结果?
XML 文件:
<something>test1234567test</something>
代码:
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{ if([currentElement isEqualToString:@"something"])
[currentname appendString:string];
[mutarray_xml addObject:currentname];
}
我尝试过:
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([currentElement isEqualToString:@"something"])
{
[currentname release];
}
}
但它显示:
test1234567test
test1234567test
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这篇关于 Big Nerd Ranch 的文章教会了我很多关于使用 NSXMLParser 解析 XML 的知识。
This post on Big Nerd Ranch taught me a lot about using NSXMLParser to parse XML.
为了正确地进行这样的解析,您还需要实现
didStartElement
方法。您不应该在foundCharacters
方法内向可变数组添加任何内容 - 您应该只在didEndElement
方法中向数组添加内容,因为此方法指示整个内容该元素已被读取。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 thefoundCharacters
method - you should only add something to the array in thedidEndElement
method, because this method indicates that the entire contents of the element has been read.