NSXMLParser问题:不获取所有数据吗? [英] NSXMLParser issue : don't get all data?

查看:86
本文介绍了NSXMLParser问题:不获取所有数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要从XML文件获取数据的应用程序.有些节点有很多字符,使用此功能时我遇到了问题:

I'm developing an application that needs to get data from an XML file. Some of the nodes have a lot of characters and I've a problem using this function :

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

例如,对于一个项目的描述节点,我将仅获得20-30个最后一个字符,而我将获得200或300个字符.

For example for the description node of an item I will get only the 20-30 last characters whereas I would get 200 or 300.

我用NSLog进行了检查,看来问题出在这里.你知道怎么了吗?

I checked this out with a NSLog and it appears the problem comes from here. Do you know what's wrong ?

谢谢您的建议.

推荐答案

SAX解析器不能保证一次获取所有字符.您可能会从任意给定的块中收到带有字符块的多个调用;您的代码应将它们串联为一个字符串.

SAX parsers do not guarantee to get all characters at once. You may get multiple calls with chunks of characters from any given block; your code should concatenate them into a single string.

解析器对象可以向委托发送多个parser:foundCharacters:消息,以报告元素的字符.因为字符串可能只是当前元素的全部字符内容的一部分,所以应将其附加到当前的字符累积中,直到元素更改为止.

The parser object may send the delegate several parser:foundCharacters: messages to report the characters of an element. Because string may be only part of the total character content for the current element, you should append it to the current accumulation of characters until the element changes.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    if ([qualifiedName isEqualToString:@"myTag"]) {
        buf = [NSMutableString string];
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([qualifiedName isEqualToString:@"myTag"]) {
        buf = [buf stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSLog(@"Got %@", buf);
    }
}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [buf appendString:string];
}

这篇关于NSXMLParser问题:不获取所有数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆