【问题标题】:How to get the right tags from a parse XML in Objective C如何从Objective C中的解析XML中获取正确的标签
【发布时间】:2014-06-04 19:15:26
【问题描述】:

我在目标 C 中非常新,我一直在尝试解析我不拥有的 XML 文件......我希望你们能帮助我。

这是 XML 示例

<rss version="2.0" xmlns:media="http://search.website.com/mrss/">

<channel>
    <title><![CDATA[This is the title of the feed]]></title>

    <link><![CDATA[http://ThiIsTheLink/OfTheWebSite/]]></link>

    <description><![CDATA[This is the description of the feed]]></description>

    <language><![CDATA[es]]></language>

    <copyright><![CDATA[Copyright WebSite]]></copyright>

    <ttl><![CDATA[10]]></ttl>

    <image>
        <url><![CDATA[http://thiIs/thelogo/ofthefeed/images/logo.png]]></url>

        <title><![CDATA[this anohter title]]></title>

        <link><![CDATA[http://ThiIsTheLink/OfTheWebSite/]]></link>
    </image>

    <item>
        <title><![CDATA[this is the title of the first new]]></title>

        <link><![CDATA[http://this is the link of the first whole new.html]]></link>

        <description><![CDATA[<p>this is the description of the first new</p>]]></description>

        <guid isPermaLink="true"><![CDATA[http://this is another link of the first whole new.html]]></guid>

        <pubDate><![CDATA[Fri, 18 Apr 2014 13:53:57 -0600]]></pubDate>
    </item>

    <item>
        <title><![CDATA[this is the title of the second new]]></title>

        <link><![CDATA[http://this is the link of the second whole new.html]]></link>

        <description><![CDATA[<p><img src="http://thi is a link of the image for the new.jpg" width="141" height="114" /></p><p>this is the description of the seconde new.</p>]]></description>

        <guid isPermaLink="true"><![CDATA[http://this is another link of the second whole new.html]]></guid>

        <pubDate><![CDATA[Thu, 17 Apr 2014 00:00:00 -0600]]></pubDate>

        <enclosure url="http://thi is a link of the image for the new.jpg" length="5823" type="image/jpeg"/>

        <media:content url="http://thi is a link of the image for the new.jpg" type="image/jpeg" fileSize="5823" width="141" height="114"/>

        <media:title><![CDATA[thi is the footnote of eht note]]></media:title>

        <media:thumbnail url="http://thi is a link of the image for the new.jpg" width="95" height="70"/>

        <media:keywords><![CDATA[keywords, keywords, keywords, keywords, keywords]]></media:keywords>
    </item>

 <channel>
 <rss>

我尝试在这里解析...但我不知道如何处理所有标签...

    NSFileHandle *loadedFileXML = [NSFileHandle fileHandleForReadingAtPath:[documentsDirectoryFeedNum stringByAppendingPathComponent:@"NacionXML.xml"]];
    NSData *dataFileXML = [loadedFileXML readDataToEndOfFile];
    NSXMLDocument *xmlDOC = [[NSXMLDocument alloc]initWithData:dataFileXML options:NSXMLDocumentTidyXML error:NULL];
    NSXMLElement *rootElement = [xmlDOC rootElement];

    NSArray *sections = [rootElement elementsForName:@"channel"];

    for (int i = 0; i < [sections count]; i++)
    {
        //I can figure it out what can I do here... sorry guys, please help!
    }

我想从 XML 中的每个 &lt;item&gt; 中提取 &lt;title&gt;&lt;link&gt;&lt;description&gt;&lt;enclosure url=".../&gt;&lt;media:title&gt; ......但我被困了 3 天...... 我读了这个问题Parsing XML Inner XML tags Objective-C 和这段代码:

NSDictionary *dictXML= [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
NSArray *arrStation = [[dictXML objectForKey:@"stations"] objectForKey:@"station"];//this would return the array of station dictionaries


for(int i=0;i<[arrStation count];i++){
   NSDictionary *aStation = [arrStation objectAtIndex:i];
   NSLog(@"id = %@",[aStation objectForKey:@"id"]);
}

//Less code version for the loop

for(NSDictionary *aStation in arrStation){
  NSLog(@"id = %@",[aStation objectForKey:@"id"]);
}

但可以采取/显示任何我想要的标签...我希望你们能帮助我。提前致谢。

【问题讨论】:

    标签: objective-c xml parsing tags


    【解决方案1】:

    首先,XML 文件格式错误,最后两个结束标记 channel,rss 缺少 /NSXMLDocument 不会解析该 XML。

    假设这是一个错字(如果不是,这种方法根本不起作用),这段代码可能会让你开始:

    NSError         *error=nil;
    NSXMLDocument   *xmlDOC=[[NSXMLDocument alloc]
        initWithContentsOfURL:[NSURL fileURLWithPath:xmlPath]
        options:NSXMLNodeOptionsNone
        error:&error
    ];
    
    if(!xmlDOC)
    {
        NSLog(@"Error opening '%@': %@",xmlPath,error);
    
        return;
    }
    
    NSXMLElement    *rootElement=[xmlDOC rootElement];
    NSArray         *items=[rootElement nodesForXPath:@"channel/item" error:&error];
    
    if(!items)
    {
        NSLog(@"Can't get 'channel/item': %@",error);
    
        return;
    }
    
    for(NSXMLElement *item in items)
    {
        NSLog(@"title: %@",[[[item nodesForXPath:@"title" error:&error]firstObject]stringValue]);
        NSLog(@"link: %@",[[[item nodesForXPath:@"link" error:&error]firstObject]stringValue]);
        NSLog(@"description: %@",[[[item nodesForXPath:@"description" error:&error]firstObject]stringValue]);
    }
    

    【讨论】:

    • 嘿@Gerd K 它就像一个魅力!有几件事,是的,我发布的 xml 有一个错字......这很好,但也许你可以比你做的更多地帮助我,我需要(以你教我的同样方式)这个标签 @ 987654326@。如果您想查看link,这里是 RSS 提要提前谢谢!
    • 我想您是在谈论缩略图 URL。 URL 实际上是一个属性(它在标签中),您可以像这样访问它:NSLog(@"thumbnail url: %@",[[[item nodesForXPath:@"media:thumbnail" error:&amp;error]firstObject]attributeForName:@"url"]);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多