Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing XML with tags with same name on different levels in DOM
    primarykey
    data
    text
    <p>I've been at this for days and cannot find a way to parse this XML file. I've tried XMLPullParser, SAX and now DOM, but as soon as I'd made some progress, I realized that I have two different <code>&lt;con&gt;</code> tags. I'm new to Java and Android, and especially new to XML parsing, so any help would mean a lot.</p> <p>Here's a segment of the xml file:</p> <pre><code>&lt;data&gt; &lt;con id="f3cVQQjz8jr"&gt; &lt;con idref="nJ4haotQTo0"/&gt; &lt;added order="9"&gt;2013-08-22T03:14:13.439Z&lt;/added&gt; &lt;name&gt;Alex&lt;/name&gt; &lt;rank&gt;0&lt;/rank&gt; &lt;/con&gt; &lt;con id="nJ4haotQTo0"&gt; &lt;added order="10"&gt;2013-08-22T03:14:13.439Z&lt;/added&gt; &lt;name&gt;Charley&lt;/name&gt; &lt;rank&gt;-2&lt;/rank&gt; &lt;/con&gt; &lt;con id="jadh7bH25mI"&gt; &lt;added order="11"&gt;2013-08-22T03:14:13.439Z&lt;/added&gt; &lt;name&gt;David&lt;/name&gt; &lt;rank&gt;1227133510&lt;/rank&gt; &lt;/con&gt; &lt;con id="erfhZ_dn0HA"&gt; &lt;con idref="nJ4haotQTo0"/&gt; &lt;added order="12"&gt;2013-08-22T03:14:13.439Z&lt;/added&gt; &lt;name&gt;Sebastien&lt;/name&gt; &lt;rank&gt;1073741824&lt;/rank&gt; &lt;/con&gt; &lt;/data&gt; </code></pre> <p>As you can see, not all sections have a child <code>&lt;con&gt;</code> tag, so I don't think using a counter would work.</p> <p>Here's my latest attempt (doesn't do anything about the nested <code>&lt;con&gt;</code> tags):</p> <pre><code> public void parseContext() { NodeList nodeList = doc.getElementsByTagName("con"); int size = nodeList.getLength(); for(int i = 0 ; i &lt; size ; i++) { System.out.println("---------------Context ("+i+")--------------------"); Node node = nodeList.item(i); if(node.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) node; NodeList resultNodeList = e.getElementsByTagName("name"); int resultNodeListSize = resultNodeList.getLength(); for(int j = 0 ; j &lt; resultNodeListSize ; j++ ) { Node resultNode = resultNodeList.item(j); if(resultNode.getNodeType() == Node.ELEMENT_NODE) { Element resultE = (Element) resultNode; System.out.println("Context Name :"+resultE.getTextContent()); } } } } } </code></pre> <p>Which results in:</p> <pre><code>---------------Context (0)-------------------- Context Name :Alex ---------------Context (1)-------------------- Context Name :Charley ---------------Context (2)-------------------- ---------------Context (3)-------------------- Context Name :David ---------------Context (4)-------------------- Context Name :Sebastien ---------------Context (5)-------------------- ... </code></pre> <p>What I'd like to have is something like this:</p> <pre><code>---------------Context (0)-------------------- Context Name :Alex Context ID Ref: duRjfksjf0 ---------------Context (1)-------------------- Context Name :Charley Context ID Ref: null ---------------Context (3)-------------------- Context Name :David Context ID Ref: null ---------------Context (4)-------------------- Context Name :Sebastien Context ID Ref: iJ4hasftQTo0 ---------------Context (5)-------------------- .... </code></pre> <p>Again, the document is much longer, and there is no pattern between whether or not there is an <code>idref</code>. I will eventually be parsing all the data within the parent <code>&lt;con&gt;</code> tags, but I would be able to figure that out if I knew how to deal with the child <code>&lt;con&gt;</code> tag.</p> <p>I searched and found this: <a href="https://stackoverflow.com/questions/13245874/how-to-differentiate-parent-and-child-tag-while-dom-parsing">How to differentiate parent and child tag while dom parsing?</a> though there were no helpful answers. No questions I found involved tags with same names on different levels. I know there're a lot of similar questions, and will understand if this gets deleted. Though again, I'd really appreciate the help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload