私はこのような 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