开发者

How to implement TouchXml Parser?

I have implemented demo using TouchXml parser.Its working fine.But I want to pars开发者_开发技巧e 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?


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜