tags:

views:

337

answers:

1

i have implement demo using TouchXml parser.Its working fine.But i want 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?

+2  A: 

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?

lyonanderson
very good answer . I have asked the same question but i was not answered properly . Have you any example project so I can understand it well ?
hib