I am parsing an XML document using DOM in Java. The data looks like this:
<nodes totalCount="48" count="10">
<node type="A" id="83" label="label1">
<record>new</record>
<createTime>12345</createTime>
</node>
<node type="A" id="77" label="label2">
<record>new</record>
<createTime>4567</createTime>
</node>
</nodes>
This is the relevant portion of my code that I am using to parse:
Document doc = dBuilder.parse(new InputSource(reader));
doc.getDocumentElement().normalize();
Log.w(TAG, "Dom Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("node");
for (int temp = 0; temp < nList.getLength();temp++) {
Element element = (Element) nList.item(temp);
NodeList time = element.getElementsByTagName("createTime");
Element line = (Element) time.item(0);
String value =getDataFromElement(line);
Log.w(TAG, "Create time: " + value );
}
And my getDataFromElement method is
public static String getDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}
My problem is, it gives null pointer exception after printing the first value of createTime at
getDataFromElement() method
Can anyone help me diagnose this issue?