4

我需要解析一个 Xml 文档并将值存储在文本文件中,当我解析普通数据(如果所有标签都有数据)然后它工作正常,但如果任何标签没有数据然后它抛出“空指针异常”我需要什么要做,为了避免空指针异常,请用示例代码向我建议示例 xml:

<company>
    <staff>
        <firstname>John</firstname>
        <lastname>Kaith</lastname>
        <nickname>Jho</nickname>
        <Department>Sales Manager</Department>
    </staff>
    <staff>
        <firstname>Sharon</firstname>
        <lastname>Eunis</lastname>
        <nickname></nickname>
        <Department></Department>
    </staff>
    <staff>
        <firstname>Shiny</firstname>
        <lastname>mack</lastname>
        <nickname></nickname>
        <Department>SAP Consulting</Department>
    </staff>
</company>

代码:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFile {

    public static void main(String argv[]) {

      try {

        File fXmlFile = new File("c:\\file.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getElementsByTagName("staff");
        System.out.println("-----------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

           Node nNode = nList.item(temp);
           if (nNode.getNodeType() == Node.ELEMENT_NODE) {

              Element eElement = (Element) nNode;

              System.out.println("First Name : " + getTagValue("firstname", eElement));
              System.out.println("Last Name : " + getTagValue("lastname", eElement));
                  System.out.println("Nick Name : " + getTagValue("nickname", eElement));
              System.out.println("Salary : " + getTagValue("Department", eElement));

           }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
  }

  private static String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

        Node nValue = (Node) nlList.item(0);

    return nValue.getNodeValue();
  }

}
4

4 回答 4

4

只需检查对象不是null

private static String getTagValue(String tag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(tag).item(0).getChildNodes();
    Node nValue = (Node) nlList.item(0);
    if(nValue == null) 
        return null;
    return nValue.getNodeValue();
}

String salary = getTagValue("Department", eElement);
if(salary != null) {
    System.out.println("Salary : " + getTagValue("Department", eElement));
}
于 2012-08-06T12:48:58.007 回答
1
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

上面的行获取具有给定标签名称的元素的子节点。如果该元素没有数据,则返回的节点列表为空。

Node nValue = (Node) nlList.item(0);

上面的行从空节点列表中获取第一个元素。由于列表为空,因此 0 是无效索引,根据文档,返回 null

return nValue.getNodeValue();

因此,上面的行调用了一个空变量上的方法,这会导致 NPE。

您应该测试列表是否为空,并返回您想要的(例如空字符串),如果是这样的话:

if (nList.getLength() == 0) {
    return "";
}
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
于 2012-08-06T12:52:47.770 回答
0

此代码应该可以工作:

// getNode function
private static String getNode(String sTag, Element eElement) {
    //if sTag exists(not null) I get childNodes->nlList
    if (eElement.getElementsByTagName(sTag).item(0)!=null){
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        //check if child (nlList) contain something
        if ((nlList.getLength() == 0))//if the content is null
            return "";
        //if child contains something extract the value of the first element
        Node nValue = (Node) nlList.item(0);
            return nValue.getNodeValue();
    }
    return "";
}
于 2014-09-06T20:27:49.667 回答
0

Replace

System.out.println("First Name : " + getTagValue("firstname", eElement));

by

System.out.println("First Name : " + getTagValue("firstname", eElement) == null?"":getTagValue("firstname", eElement));

same thing for the other tags.

于 2012-08-06T12:51:32.287 回答