Question

im very new in objective C and im stuck trying to parse a XML file, that I dont own... I hope you guys can help me.

This is the XML Example

<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>

and i try to parse here... but I dont know how to handle all the tags...

    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!
    }

I want to take <title>, <link>, <description>, <enclosure url=".../> and <media:title> from each <item> inside the XML... but I been stuck 3 days... I read this question Parsing XML Inner XML tags Objective-C and this code:

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"]);
}

But can take/display any tag that i want... I hope you guys can help me. Thanks in advance.

Was it helpful?

Solution

Firstly, the XML file is malformed, the last two closing tags channel,rss are missing the /.NSXMLDocument will not parse that XML.

Assuming that was a typo (if not, this approach will not work at all), this code may get you started:

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]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top