tags:

views:

754

answers:

1

Hi everyone,

I've been wondering if there's a way to parse XML in Objective C as easily as parsing with SimpleXML in PHP.

The only existing methods I know are the following:

  1. Use of NSXMLParser and create a delegate to handle the events triggered upon encountering XML elements, values, etc...

  2. Use of libxml2 based framework such as TouchXML, to make use of the XPath to easily query nodes in the XML file.

I'm not sure if there are any other approach to parsing XML in Objective C for iPhone.

Thanks in advance for the help! :)

Cheers! Oonoo

Update: SimpleXML sample

In case unfamiliar with SimpleXML, it can be as easy as this:

$xml = simplexml_load_string($xmlfilecontents); 
echo $xml->user->name; //for this example code, im not traversing through all the user elements

Given that the xmlfilecontents is an xml file content in string type, containing users elements() with child :

<users> 
<user><name>john</name></user> 
<user><name>jane</name></user> 
</users>

It simply loads the xml and instantiate automatically an object that captures all the data in the xml file. Accessing the data is a straightforward call like $xml->element->attr

+2  A: 

If you want to use libxml2 with an Objective-C front, take a look at this useful set of wrapper functions.

You issue an XPath query to your XML document object and get back Foundation class objects: NSArray, NSString, and NSDictionary, e.g.:

NSArray *queriedBuckets = PerformXMLXPathQuery(responseData, @"//*[local-name()='Buckets']/*[local-name()='Bucket']");

These functions help unite the speed of libxml2 with the readability and usability of Objective-C code.

Alex Reynolds
I see this could be the closest possible to how easily I want to manipulate XML docs. Thank you :) regards
oonoo