Java DOM - NULL指针异常

时间:2011-07-22 11:16:33

标签: java xml dom xml-parsing

我正在使用Java中的DOM解析XML文档。数据如下所示:

<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>

这是我用来解析的代码的相关部分:

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 );
 }

我的 getDataFromElement 方法是

public static String getDataFromElement(Element e) {
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
          CharacterData cd = (CharacterData) child;
          return cd.getData();
        }
   return "";
}

我的问题是,在

打印createTime的第一个值后,它会给出null pointer exception

getDataFromElement()方法

有人可以帮我诊断这个问题吗?

2 个答案:

答案 0 :(得分:1)

当任何“node”元素没有“createTime”-element

时,就会发生这种情况

答案 1 :(得分:0)

使用try / catch消除不需要的节点:

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