我有一个这样的 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