XML Parse 不显示带有 & 的元素里面的符号 [英] XML Parse not showing elements with & symbol inside
问题描述
我正在尝试解析我的应用程序中的 xml 提要...xml 文件在此 url 中
Im trying to parse a xml feed in my app... The xml file is in this url
feeds.feedburner.com/blogspot/TUvAW?format=xml
feeds.feedburner.com/blogspot/TUvAW?format=xml
问题是,当我访问描述段时,它会显示不同的特殊符号,例如引号.这是我试图从我的 xml 中解析的描述段之一:
The problem is that when I access the description segment it diplays different special symbols such as quotation marks. This is one of the descripton segments im trying to parse from my xml:
<description>Este es mi blog 3<img src="http://feeds.feedburner.com/~r/blogspot/TUvAW/~4/URCV9ModTR0" height="1" width="1"/></description>
我用 NSXMLParser 解析它,这些是我在我的中使用的方法:
Im parsing it with the NSXMLParser, these are the method I'm using in my:
XMLParser.m
XMLParser.m
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:@"item"])
{
currentFeed = [Feed alloc];
isStatus = YES;
}
if ([elementname isEqualToString:@"author"])
{
isStatus = NO;
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (isStatus)
{
if ([elementname isEqualToString:@"description"])
{
currentFeed.description = currentNodeContent;
NSLog(@"%@",currentNodeContent);
}
if ([elementname isEqualToString:@"title"])
{
currentFeed.content = currentNodeContent;
NSLog(@"%@",currentNodeContent);
}
if ([elementname isEqualToString:@"link"])
{
currentFeed.WVUrl = currentNodeContent;
NSLog(@"%@",currentNodeContent);
}
}
if ([elementname isEqualToString:@"item"])
{
[self.feeds addObject:currentFeed];
currentFeed = nil;
currentNodeContent = nil;
}
}
我使用这些 NSLogs 来跟踪我从解析中获得的字符串,但我的描述节点内容总是显示如下:>标题和链接节点显示完美.
Im using those NSLogs to track the strings that Im getting from the parse but my description node content is always showing just this : > The title and link nodes are displaying perfectly.
我想从 description 节点中获取所有字符串以供以后使用,但我不能,我不知道这有什么问题.
I want to get all that string from the description node to use it later but simply I can't, I dont know whats going wrong with this.
推荐答案
Abhishek、Rob 和我已经概述了这些问题.但我认为值得总结一下并展示正确的解决方案.
The problems have been outlined by Abhishek, Rob and me. But I think it's worth to summarize it and show the correct solution.
主要问题是 parser:foundCharacters:
被多次调用
标签,每次调用提供一段描述.
The main problem is that parser:foundCharacters:
is called several times for the <description>
tag, each call providing a piece of the description.
解决方案是连接这些部分:
The solution is to concatenate the pieces:
XMLParser.h:
NSMutableString* currentNodeContent;
XMLParser.m:
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (currentNodeContent == nil)
currentNodeContent = [[NSMutableString alloc] initWithCapacity: 20];
[currentNodeContent appendString: [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:@"item"])
{
currentFeed = [Feed alloc];
isStatus = YES;
}
if ([elementname isEqualToString:@"author"])
{
isStatus = NO;
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (isStatus)
{
if ([elementname isEqualToString:@"description"])
{
currentTweet.description = currentNodeContent;
NSLog(@"%@",currentNodeContent);
}
if ([elementname isEqualToString:@"title"])
{
currentTweet.content = currentNodeContent;
NSLog(@"%@",currentNodeContent);
}
if ([elementname isEqualToString:@"link"])
{
currentFeed.WVUrl = currentNodeContent;
NSLog(@"%@",currentNodeContent);
}
}
if ([elementname isEqualToString:@"item"])
{
[self.feeds addObject:currentFeed];
currentFeed = nil;
[currentNodeContent release];
currentNodeContent = nil;
}
}
这篇关于XML Parse 不显示带有 & 的元素里面的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!