我有一个这样的xml,但是我的XMLParser会跳转1-2个元素,而不是处理它。我认为它工作正常,但事实并非如此。我错了什么?
这是xml,我想得到所有商品标签,以及它的子元素。
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>News</title>
<description>News - Android</description>
<link>http://www.p.com</link>
<item>
<title>World Bench Press highlights - Day 2</title>
<pubDate>Tue, 21 May 2013 00:00:00 +0000</pubDate>
<imgLink>http://www.powerlifting-ipf.com/typo3temp/pics/1b7ecb3ac3.jpg</imgLink>
<description>desc </description>
<URL> some URL </URL>
</item>
</channel>
</item>
</rss>
</channel>
这是我的XMLParser文件,但我无法获取任何节点,也没有放入我的数组中。不去了
if ([elementname isEqualToString:@"item"])
#import <Foundation/Foundation.h>
#import "XMLParser.h"
@implementation XMLParser
@synthesize tweets = _tweets;
NSMutableString *currentNodeContent;
NSMutableString *copy;
NSXMLParser *parser;
Tweet *currentTweet;
bool isStatus=YES;
-(id) loadXMLByURL:(NSString *)urlString
{
_tweets = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
NSLog(@"0");
return self;
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"1");
NSLog(@"currentnode %@",currentNodeContent);
currentNodeContent = [[NSMutableString alloc] initWithCapacity:100];
[currentNodeContent appendString:string];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"2 Elementname : %@",elementname);
if ([elementname isEqualToString:@"item"])
{
currentTweet = [Tweet alloc];
isStatus = YES;
}
if ([elementname isEqualToString:@"title"])
{
NSLog(@"%@",currentNodeContent);
currentTweet.content = [NSString stringWithFormat:@"%@ %@",currentTweet.content ,currentNodeContent];
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"ELEMENT: %@",elementname);
if ([elementname isEqualToString:@"description"])
{
currentTweet.description = currentNodeContent;
}
if ([elementname isEqualToString:@"pubDate"])
{
currentTweet.dateCreated = currentNodeContent;
}
if ([elementname isEqualToString:@"title"])
{
currentTweet.title = currentNodeContent;
}
if ([elementname isEqualToString:@"imgLink"])
{
currentTweet.imgLink = currentNodeContent;
}
if ([elementname isEqualToString:@"URL"])
{
currentTweet.URL = currentNodeContent;
}
if ([elementname isEqualToString:@"item"])
{
NSLog(@"%@ elem:",currentNodeContent);
[self.tweets addObject:currentTweet];
currentTweet = nil;
currentNodeContent = nil;
}
}
@end
答案 0 :(得分:2)
您的方法是错误的,因为每次XML解析器找到新字符时都会重新分配可变字符串。这意味着如果你得到一个像@“Hello”这样的字符串,并且字符串被解析成两个独立的字符串@“He”和@“llo”,只需将@“llo”放入可变字符串中。正确的方法是每次找到新标记时分配一个新的可变字符串,而不是在找到新字符时。
不确定,因为我不知道整个上下文,但我会尝试用这种方式编辑前两种方法:
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"1");
NSLog(@"currentnode %@",currentNodeContent);
[currentNodeContent appendString:string];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentNodeContent= [NSMutableString new];
NSLog(@"2 Elementname : %@",elementname);
if ([elementname isEqualToString:@"item"])
{
currentTweet = [Tweet alloc];
isStatus = YES;
}
}
让我知道你得到了什么结果。我还建议在每个方法中加入断点,以查看(在这种情况下)为什么你得到一些特定的字符串而不是预期的结果。
修改
对于XML,我尝试用xmllint检查它,并且它有一些错误。关闭频道标签后,问题是这两个额外的行:
</item> <!-- Not needed -->
</rss>
</channel> <!-- Not needed -->
答案 1 :(得分:0)
你和之间有一个。也许该标记可能会破坏解析过程,尝试删除它