Objective c 如何在Objective C中从解析XML中获得正确的标记
我对objective C非常陌生,我一直在尝试解析一个我不拥有的XML文件。。。我希望你们能帮助我 这是XML示例Objective c 如何在Objective C中从解析XML中获得正确的标记,objective-c,xml,parsing,tags,Objective C,Xml,Parsing,Tags,我对objective 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><![
<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!
}
NSFileHandle*loadedFileXML=[NSFileHandle fileHandleForReadingAtPath:[documentsDirectoryFeedNum stringByAppendingPathComponent:@“NacionXML.xml”];
NSData*dataFileXML=[loadedFileXML readDataToEndOfFile];
NSXMLDocument*xmlDOC=[[NSXMLDocument alloc]initWithData:dataFileXML选项:NSXMLDocumentTidyXML错误:NULL];
NSXMLElement*rootElement=[xmlDOC rootElement];
NSArray*sections=[rootElement元素名称:@“通道”];
对于(int i=0;i<[节计数];i++)
{
//我能弄明白我能在这里做什么…对不起,伙计们,请帮帮我!
}
首先,XML文件格式不正确,最后两个结束标记
频道、rss
缺少/
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有一个输入错误。。。这太棒了,但也许你能比你帮助我更多,这是我需要(以你教我的方式)这个标签
我想你说的是缩略图URL。URL实际上是一个属性(它在标记中),您可以这样访问它:NSLog(@“缩略图URL:%@”,[[[item nodesForXPath:@“media:缩略图”错误:&error]firstObject]attributeForName:@“URL]”)代码>
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]);
}