0

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?

8
  • FYI iam using javax.xml.parsers... and org.w3c.dom.... classes. Commented Jul 22, 2011 at 11:21
  • 2
    if you respected people's time a little more, you would give us a SSCCE and wouldn't have had to explain that. Commented Jul 22, 2011 at 11:24
  • 1
    which row exactly throws this exception? Commented Jul 22, 2011 at 11:29
  • the line Node child = e.getFirstChild() throws it.Also it prints the createTime first time then throws the exception.. Commented Jul 22, 2011 at 11:35
  • 2
    For what its worth, your code compiles and runs for me with no problems. Commented Jul 22, 2011 at 12:24

2 Answers 2

1

It happens when any "node"-element has no "createTime"-element

Sign up to request clarification or add additional context in comments.

2 Comments

well all the node elements have createTime elements within them
Yup its because another node element is encountered before createTime element in the XML as we go down..
0

Use try/catch to eliminate unwanted nodes:

jcomeau@intrepid:/tmp$ cat so.xml ElementTest.java; java ElementTest
<?xml version="1.0" encoding="UTF-8"?>
<nodes totalCount="48" count="10">
<node type="A" id="83" label="label1">
<record>new</record>
<createTime>12345</createTime>
<node type="B">
</node>
</node>
<node type="A" id="77" label="label2">
<record>new</record>
<createTime>4567</createTime>
</node>
</nodes>
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.*;
public class ElementTest {
 public static void main(String args[]) throws Exception {
  InputStream reader = ElementTest.class.getResourceAsStream("so.xml");
  DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance(
   ).newDocumentBuilder();
  Document doc = dBuilder.parse(new InputSource(reader));
  doc.getDocumentElement().normalize();
  System.out.println("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);
   try {
    String value =getDataFromElement(line);  
    System.out.println("Create time: " + value );
   } catch (Exception problem) {
    System.err.println("problem element " + line + ": " + problem);
   }
  }
 }
 public static String getDataFromElement(Element e) {
  Node child = e.getFirstChild();
  if (child instanceof CharacterData) {
   CharacterData cd = (CharacterData) child;
   return cd.getData();
  }
  else return "";
 }
}
Dom Root element :nodes
Create time: 12345
problem element null: java.lang.NullPointerException
Create time: 4567

7 Comments

Just add another <node> element within a node element it throws null pointer exception.
Hey thats not in my hands, i receive it as a template from the server and my program has to parse it.
Thanks for the help jcomeau_ictx, but what if i want to even get that node value as well?
extract whatever value you want from it, just make sure you trap your exceptions in case it's not what you expect. got to go work out now, good luck.
Thanks dude.. And congrats for the GOLD!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.