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

最佳答案

您的方法是错误的,因为每次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 -->

10-07 22:46