Search code examples
iphoneobjective-cparsinglibxml2nsxml

libxml2 example (with wrapper)


I am using the XML parser libxml2 with wrapper as given on the page http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html

But i am not sure if I am using correctly and am getting errors (parsing,etc)

So could someone please provide me a complete example which i can refer and get an idea if I am doing something incorrectly.

thanks a lot for all your help in advance.


Solution

  • I'm using this methods too, to parse xml and html files. For example to parse rss xml:

    //add xml source
    NSURL *url = [NSURL URLWithString:@"http://feeds.bbci.co.uk/news/rss.xml?edition=int"];     
    NSData *xmlData = [NSData dataWithContentsOfURL:url];
    
    //parse the whole file with all tags
    NSArray *rssFeedArray = PerformXMLXPathQuery(xmlData, @"//*");
    NSLog(@"rssFeedArray: %@", rssFeedArray);
    

    //* - query means the parser will go through all tags of the file. Then log the array to see the whole structure of xml.

    Whith '/rss/channel/item' query you will only get the item tags element's, (or to get only the first item use '/rss/channel/item[1]').

    in this case because of the bbc feed structure you can catch each item title at

    [[[[rssFeedArray objectAtIndex:i] valueForKey:@"nodeChildArray"] objectAtIndex:0] valueForKey:@"nodeContent"]]
    

    and description at

    [[[[rssFeedArray objectAtIndex:i] valueForKey:@"nodeChildArray"] objectAtIndex:1]valueForKey:@"nodeContent"]]
    

    and go on and on.