I was trying to parse a collada(.dae) file in java using plane DOM parser. When I try to get value it returns me null. I tried with answers and suggestions from other discussions but was not a success. The code I used is below.
for(int k1=0;k1<meshlist.getLength();k1++) {
Element geometryItr1 = (Element)geometrylist.item(k);
NodeList trianglelist = geometryItr1.getElementsByTagName("triangles");
//System.out.println("Triangles length is " + trianglelist.getLength());
for(int o=0;o<trianglelist.getLength();o++) {
Element trichildnodes = (Element) trianglelist.item(o);
NodeList inputs = trichildnodes.getElementsByTagName("input");
NodeList p = trichildnodes.getElementsByTagName("p");
Element ppp = (Element) p.item(0);
System.out.println("Node Value " + ppp.getNodeValue());
System.out.println(inputs.getLength() + "Input length");
for(int in=0;in<inputs.getLength();in++) {
Element inn = (Element) inputs.item(in);
System.out.println(inn.getAttribute("semantic") + " " + inn.getAttribute("source") + " Attributes");
}
//System.out.println(p.getLength() + " P's length" );
//System.out.println("P's content " + ppp.getFirstChild().getNodeValue());
}
}
The XML is very large and I am posting a part which I was trying to parse.
<mesh>
<source> </source>
<source> </source>
<source> </source>
<triangles>
<input />
<input />
<input />
<p> 24 262 2 72 72 72 72 2222 8198219 </p>
<triangles>
<triangles>
<input />
<input />
<input />
<p> 24 262 2 72 72 72 72 2222 8198219 </p>
<triangles>
<triangles>
<input />
<input />
<input />
<p> 24 262 2 72 72 72 72 2222 8198219 </p>
<triangles>
<triangles>
<input />
<input />
<input />
<p> 24 262 2 72 72 72 72 2222 8198219 </p>
<triangles>
</mesh>
I was trying to get the value of <p>. Everything works fine except getting p’s value. But when I debug I can see the values, its associated with first child. I even tried using firstChild. I am completely lost with parsing trying to find out a solution on this. Please some one help me find a solution on How to get the value of p ?
When I use getTextContent instead I get the output like below:
NodeValue null
NodeValue 24 262 2 72 72 72 72 2222 8198219
NodeValue null
The output is blank for two tags.
I would recommend using the
javax.xml.xpathAPIs available in the JDK/JRE since Java SE 5 to make the processing of your XML document easier:input.xml
Output
UPDATE
You could also get the
pelements using the following line of code. You need to be careful though since it will get allpelements not just those in the/mesh/triangles/ppathThe following approach will always get you the data you are looking for, even if
peleements are later added somewhere else in the document.