Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement TouchXml Parser?

Tags:

iphone

I have implemented demo using TouchXml parser.Its working fine.But I want to parse xml like below.

example:

<Root>
<tag1></tag1>
<tag2>
      <temp1></temp1>
      <temp2></temp2>
      <temp3></temp3>
</tag2>
<tag3></tag3>

</Root>

How to parse this type of example?

like image 691
Mansi Avatar asked Dec 17 '25 22:12

Mansi


1 Answers

TouchXML is nice and easy to use. First you'll want to parse the document:

NSError *theError = NULL;
CXMLDocument *theXMLDocument = [[[CXMLDocument alloc] initWithXMLString:input options:0 error:&theError] autorelease];

You can then query the structure of your document using XPath. For example to extract the Root element you might do this:

NSArray *foundRoots = [theXMLDocument nodesForXPath:@"//Root"  error:&theError]; 
CXMLElement *root = [foundRoots objectAtIndex:0];

(You often get arrays back, so in your case you can just take the first element, assuming it exists in the document)

You can also do things like get all the child elements of an element. So if we wanted to get all the tags we could this:

NSArray *children = [root children];

Or we could get a tag with a particular name:

NSArray *tag1 = [root elementsForName:@"tag1"];

(Again you get an array, so do the right thing and check)

Does your data conform to any schema?

like image 160
lyonanderson Avatar answered Dec 23 '25 07:12

lyonanderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!