NSXMLParser 从 XML 标签中检索错误数据
我正在读取带有某些字段的 xml,例如:
<url>http://myurl.com/noticias/redirect.html?document=xx222&usuario=44yyy&url=http%3A%2F%2Fwww.environmental-expert.com%2FresultEachPressRelease.aspx%3Fcid%3D23745%26codi%3D234441%26lr%3D1</url>
<title>The world water report – supplies falling, Tensions Rising</title>
输出为:
2011-04-28 17:08:02.191 MyProject[12093:207] doc found
2011-04-28 17:08:02.192 MyProject[12093:207] url=http%3A%2F%2Fwww.environmental-expert.com%2FresultEachPressRelease.aspx%3Fcid%3D23745%26codi%3D234441%26lr%3D1
2011-04-28 17:08:02.193 MyProject[12093:207] – supplies falling, Tensions Rising
这与 xml 文件上的数据不匹配。
我的解析器有以下方法:
// Start of element
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"document"]){
NSLog(@"doc found");
}
}
// Found Character
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string
{
currentNodeContent = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
// End Element
- (void) parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"url"]){
NSLog(@"%@", currentNodeContent);
}
if ([elementName isEqualToString:@"title"]){
NSLog(@"%@", currentNodeContent);
}
if ([elementName isEqualToString:@"document"]){
[currentNodeContent release];
currentNodeContent = nil;
}
}
它可以与函数foundCharacters相关吗?
预先感谢您的帮助!
I am reading a xml with some fields, for example:
<url>http://myurl.com/noticias/redirect.html?document=xx222&usuario=44yyy&url=http%3A%2F%2Fwww.environmental-expert.com%2FresultEachPressRelease.aspx%3Fcid%3D23745%26codi%3D234441%26lr%3D1</url>
<title>The world water report – supplies falling, Tensions Rising</title>
And the output is:
2011-04-28 17:08:02.191 MyProject[12093:207] doc found
2011-04-28 17:08:02.192 MyProject[12093:207] url=http%3A%2F%2Fwww.environmental-expert.com%2FresultEachPressRelease.aspx%3Fcid%3D23745%26codi%3D234441%26lr%3D1
2011-04-28 17:08:02.193 MyProject[12093:207] – supplies falling, Tensions Rising
which is not matching the data on the xml file.
my parser has the following methods:
// Start of element
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"document"]){
NSLog(@"doc found");
}
}
// Found Character
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string
{
currentNodeContent = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
// End Element
- (void) parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"url"]){
NSLog(@"%@", currentNodeContent);
}
if ([elementName isEqualToString:@"title"]){
NSLog(@"%@", currentNodeContent);
}
if ([elementName isEqualToString:@"document"]){
[currentNodeContent release];
currentNodeContent = nil;
}
}
Can it be related to the function foundCharacters?
Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现一个字段的字符可以被调用多次!
您应该创建一个临时 NSMutableString 并附加 findcaracters 结果!
请务必将 currentNodeContent 声明为 NSMutableString 而不是 NSString !
这是我在项目中使用的 xml 解析器代码:
-
-
-
如下使用(此示例用于分析 rss feed:
如果需要更复杂的分析,请检查 XMLNode.h 以获取更多方法
foundCharacters can be called several times for one field !
You should create a temporary NSMutableString and append foundcaracters result !
Be sure to declare currentNodeContent as a NSMutableString and not a NSString !
Here is the xml parser code I'm using in my projects :
-
-
-
use this as following (this sample is to analyze a rss feed :
Check XMLNode.h for more methods if you need a more complex analysis