How to do a parsing of nested nodes with NSXMLParser. Since this parser is know to parse XML node-wise manner, how should we handle multiple node parsing with same names like below:
<data>
<nodesame attrisame="this is same">
<nodesame> this is also same </nodesame>
<nodedifferent> this is different although </nodedifferent>
</nodesame>
</data>
In above we have a master data node which contains nodesame node which contains another nodesame node inside. Please tell me as to how differentiate both nodesame with different context.
Please suggest me some good way for getting the dictionary/array from the delegate:
parser:didEndElement:namespaceURI:qualifiedName
I am famaliar with libxml2 and touchXML but using NSXMLParser is making me crazy plus we have to use the same because the code we have uses the NSXMLParser, so changing it to Libxml will take a hell.
When you use nsxmlparser, it goes from top to bottom, and it calls matching pairs of
didStartElementanddidEndElement. So, for your first question (how to know when you are within a child node of the same name as a parent node?), the answer is to keep track of whendidStartElementwas called, followed by anotherdidStartElementof the same node name without a call todidEndElementin between.Now, each element is named so an array isn’t really applicable for general parsing. It’s only viable in a JSON response.
So for your second question, (how to create an array or dictionary of the xml?), is to let each node be a dictionary of its own, perhaps with a key of “attributes” that’s another dictionary. I would create something like this for your example:
One of the problems here is occupying the words “attributes” and “text”, so it’s not a pretty solution.