RSS feed XML& ATOM解析问题 - GData - iPhone

时间:2011-03-12 13:45:45

标签: iphone xml rss xml-parsing gdata

我有一个关于解析XLM文档更准确地解析ATOM格式数据的问题。 (RSS feed)。

以下是xlm文档的示例:

<item>
            <title>Panty + Stocking Print Preview</title>
            <link>http://SonicRocksMySocks.deviantart.com/art/Panty-Stocking-Print-Preview-200615179</link>
            <guid isPermaLink="true">http://SonicRocksMySocks.deviantart.com/art/Panty-Stocking-Print-Preview-200615179</guid>
            <pubDate>Fri, 11 Mar 2011 21:43:34 PST</pubDate>
            <media:title type="plain">Panty + Stocking Print Preview</media:title>

            <media:keywords></media:keywords>
            <media:rating>nonadult</media:rating>
            <media:category label="Movies &amp; TV">fanart/digital/drawings/movies</media:category>
            <media:credit role="author" scheme="urn:ebu">SonicRocksMySocks</media:credit>
            <media:credit role="author" scheme="urn:ebu">http://a.deviantart.net/avatars/s/o/sonicrocksmysocks.png?15</media:credit> 
            <media:copyright url="http://sonicrocksmysocks.deviantart.com">Copyright 2011 *SonicRocksMySocks</media:copyright>
            <media:thumbnail url="http://th00.deviantart.net/fs71/300W/i/2011/070/d/c/panty___stocking_print_preview_by_sonicrocksmysocks-d3bfvnv.png" height="351" width="300"/>

            <media:thumbnail url="http://th02.deviantart.net/fs71/150/i/2011/070/d/c/panty___stocking_print_preview_by_sonicrocksmysocks-d3bfvnv.png" height="150" width="128"/>
            <media:content url="http://th08.deviantart.net/fs71/PRE/i/2011/070/d/c/panty___stocking_print_preview_by_sonicrocksmysocks-d3bfvnv.png" height="967" width="826" medium="image"/>
            <media:content url="http://www.deviantart.com/download/200615179/" medium="document"/>
            <description><![CDATA[ The final version of this print will be available for sale at Sakura Con 2011. :3<br /><div><img src="http://th00.deviantart.net/fs71/300W/i/2011/070/d/c/panty___stocking_print_preview_by_sonicrocksmysocks-d3bfvnv.png" alt="thumbnail" /></div> ]]></description>
            <media:description type="html"><![CDATA[ The final version of this print will be available for sale at Sakura Con 2011. :3 ]]></media:description>
        </item>

这就是我解析title,pubDate和链接值的方法:

 NSArray *channels = [rootElement elementsForName:@"channel"];
for (GDataXMLElement *channel in channels) 
{            

    NSString *blogTitle = [channel valueForChild:@"title"];                    

    NSArray *items = [channel elementsForName:@"item"];
    for (GDataXMLElement *item in items)
    {

        NSString *articleTitle = [item valueForChild:@"title"];
        NSString *articleUrl = [item valueForChild:@"link"];            
        NSString *articleDateString = [item valueForChild:@"pubDate"];        
        NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];



        RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle 
                                                  articleTitle:articleTitle 
                                                    articleUrl:articleUrl 
                                                   articleDate:articleDate] autorelease];
        [entries addObject:entry];                        

    }      
}

我不知道如何检索媒体:内容行。我该怎么做? 任何人都可以帮我这个吗?

非常感谢, 安德烈

3 个答案:

答案 0 :(得分:0)

首先,您尝试使用NSXMLParsing和这个简单的教程

http://blancer.com/tutorials/i-phone/76999/parsing-xml-files/

答案 1 :(得分:0)

您是否尝试过迭代item的子项并在调试器或NSLog中检查它们?

答案 2 :(得分:0)

我知道您正在使用哪种代码。它实际上非常简单。你这样做就像获得“title”,“link”和“pubDate”一样。

 NSArray *channels = [rootElement elementsForName:@"channel"];
for (GDataXMLElement *channel in channels) 
{            

NSString *blogTitle = [channel valueForChild:@"title"];                    

NSArray *items = [channel elementsForName:@"item"];
for (GDataXMLElement *item in items)
{

    NSString *articleTitle = [item valueForChild:@"title"];
    NSString *articleUrl = [item valueForChild:@"link"];            
    NSString *articleDateString = [item valueForChild:@"pubDate"];        
    NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];

    // Right here
    NSString *mediaCont = [item valueForChild:@"media:content"];



    RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle 
                                              articleTitle:articleTitle 
                                                articleUrl:articleUrl 
                                               articleDate:articleDate] autorelease];
    [entries addObject:entry];                        

}      
}

之后,您需要更改RSSEntry的init方法以接受此新字段。您应该尝试查看定义valueForChild的代码,并了解它是如何使用GDataXML解析器的。或者您可以按照建议使用NSXML。