【发布时间】: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 中的每个 <item> 中提取 <title>、<link>、<description>、<enclosure url=".../> 和 <media:title> ......但我被困了 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