Take the 2-minute tour ×

I am trying to build an app which during start-up connects to website and downloads the XML data. Though the data is large(100 KB) and i am using TouchXml for it. The xml is like this.

<itemA attA="AAA" attB="BBB" attC="CCC">
            <itemC1 attD="DDD" attE="EEE" attF="FFF">
                         <itemE1 attG="GGG">
                                      <itemH1 attH="HHH">
                                      <itemH1 attH="III">

Here three dots ". . ." presents tens/hundreds of same kind of element. i want to extract each and every attribute and node contents. Initailly i begin with

[CXMLDocument nodesForXPath:@"//itemA" error:nil];

and able to get its attributes and upto first child nodes using -

[CXMLElement childAtIndex:index];

but how i will move further into child nodes and their nodes and get their values. Any help is greatly appreciated.link text

share|improve this question

2 Answers 2

You would probably definitely be better served with using the event-driven parser in this case; NSXMLParser.

share|improve this answer
You just need to store a bit of state in your parser; the current object that you are readying is a good example; and tell that to append any text you find in foundCharacters. –  Williham Totland Sep 17 '10 at 13:39
I also have thought to use NSXMLParser, but i am good in only extracting attribute values. I am not sure how to get node(element) content using "parser:foundCharacters:".though there are many tutorials which uses "if" or BOOL(s). How will i implement such number of if(s) for each element. –  user302326 Sep 17 '10 at 13:45
Could you maybe show some more relevant XML; (like the one actually being used), and the sort of data structure you want to build? That would make things easier to explain. And don't delete your comments, makes conversation confusing. :P –  Williham Totland Sep 17 '10 at 14:10
Yeah, you want to use NSXMLParser. –  Williham Totland Sep 20 '10 at 7:09

You can use XPaths for this:-

  NSArray *nodes = [doc nodesForXPath:@"//itemA/itemB/itemC1" error:nil]; 

This will return all the nodes in the itemC1 element. Note 'doc' is your CXMLDocument object.

Google XPath for the full syntax.

share|improve this answer
Echelon thanks for the post. I don't want to provide "nodesForXPath" for each child i am interested.I want to drill down further in the tree and accordingly i'll make arrays of dictionary/ dictionary of arrays whatever and at last come out of itemA. –  user302326 Sep 17 '10 at 13:19
OK, so to walk the tree it's a straight recursive method iterating over [nodes children]. I recall I had a bit of difficulty when I first used TouchXML until I realized CXMLElement is a subclass of CXMLNode, and once you start treating everything as a CXMLNode it makes more sense. –  Echelon Sep 17 '10 at 14:00

Your Answer

