Quick fix with XML?

2019-08-27 19:25发布

问题:

I'm currently using the YouTube API V2 and I'm getting incorrect data while gathering the description of the video.

This is the link where I'm getting the XML information from.

This is the string I'm suppose to be getting

Can we hit 5000 Likes for all the great goals? :) http://www.facebook.com/GudjonDanielYT https://twitter.com/GudjonDaniel

but I get this one

GudjonDanielhttp://gdata.youtube.com/feeds/api/users/GudjonDanielkM0YirXKa4Lb7MsgqHWiMwEntertainmentgudjondanielCan we hit 5000 Likes for all the great goals? :) http://www.facebook.com/GudjonDanielYT https://twitter.com/GudjonDaniel

My code, it's in the media:description

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementname isEqualToString:@"title"] || [elementname isEqualToString:@"content"])
    {
        currentNodeContent = [[NSMutableString alloc] init];
    }
    else if ([elementname isEqualToString:@"entry"])
    {
        currentFeed = [ChanelFeeds alloc];
    }
    else if ([elementname isEqualToString:@"yt:duration"])
    {
        currentFeed.duration = [attributeDict objectForKey:@"seconds"];
    }
    else if ([elementname isEqualToString:@"yt:statistics"])
    {
        currentFeed.views = [attributeDict objectForKey:@"viewCount"];
    }
    else if ([elementname isEqualToString:@"gd:rating"])
    {
        currentFeed.rating = [attributeDict objectForKey:@"average"];
    }
    else if ([elementname isEqualToString:@"media:player"])
    {
        currentFeed.streamURL = [NSURL URLWithString:[attributeDict objectForKey:@"url"]];
    }
    else if ([elementname isEqualToString:@"media:thumbnail"] && !currentFeed.thumbnailURL)
    {
        currentFeed.thumbnailURL = [NSURL URLWithString:[attributeDict objectForKey:@"url"]];
    }
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementname isEqualToString:@"title"])
    {
        currentFeed.title = currentNodeContent;
        currentNodeContent = nil;
    }
    else if ([elementname isEqualToString:@"media:description"])
    {
        currentFeed.description = currentNodeContent;
        currentNodeContent = nil;
    }
    else if ([elementname isEqualToString:@"entry"])
    {
        [self.feeds addObject:currentFeed];
        currentFeed = nil;
        currentNodeContent = nil;
    }
}

回答1:

The element names you're looking for in didStartElement (which initializes currentNodeContent) don't match the element names that you're using to save and reset the currentNodeContent in your didEndElement method.

Thus, your lines that say:

if ([elementname isEqualToString:@"title"] || [elementname isEqualToString:@"content"])
{
    currentNodeContent = [[NSMutableString alloc] init];
}

Probably should say:

if ([elementname isEqualToString:@"title"] || [elementname isEqualToString:@"media:description"])
{
    currentNodeContent = [[NSMutableString alloc] init];
}