XMl解析中的空指针exception

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

  John Kaith Jho Sales Manager   Sharon Eunis     Shiny mack  SAP Consulting   

码:

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

只需检查对象是否为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)); } 
 NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes(); 

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

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

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

 return nValue.getNodeValue(); 

因此,上面的行调用null变量的方法,这会导致NPE。

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

 if (nList.getLength() == 0) { return ""; } Node nValue = (Node) nlList.item(0); return nValue.getNodeValue(); 

更换

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

通过

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

其他标签也一样。

这段代码应该有效:

 // 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 ""; }