views:

112

answers:

2

Hi all, 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.

<?xml version="1.0" encoding="UTF-8"?>
<itemA attA="AAA" attB="BBB" attC="CCC">
      <itemB>
            <itemC1 attD="DDD" attE="EEE" attF="FFF">
                  <itemD>                     
                         <itemE1 attG="GGG">
                                <itemF>ZZZ</itemF>
                                <itemG>
                                      <itemH1 attH="HHH">
                                            <itemG>ZZZ</itemG>
                                            <itemH>YYY</itemH>
                                      </itemH1>
                                      <itemH1 attH="III">
                                      ...                               
                                </itemG>
                          </itemE1>
                          ...
                   </itemD> 
              </itemC1>
              ...
       </itemB>
</itemA>

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

+2  A: 

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

Williham Totland
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
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.
KayKay
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
Yeah, you want to use `NSXMLParser`.
Williham Totland
+1  A: 

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.

Echelon
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.
KayKay
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