我需要解析一个Xml文档并将值存储在文本文件中,当我解析普通数据时(如果所有标签都有数据)然后它工作正常,但如果任何标签没有数据则抛出“Null pointerException”我需要做什么,以避免空指针异常,请建议我使用示例代码 示例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();
}
}
答案 0 :(得分: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));
}
答案 1 :(得分:1)
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
上面的行获取具有给定标记名称的元素的子节点。如果此元素没有数据,则返回的节点列表为空。
Node nValue = (Node) nlList.item(0);
上面一行从空节点列表中获取第一个元素。由于列表为空,因此0是无效索引,并且根据the documentation,返回null
return nValue.getNodeValue();
上面的行调用了一个null变量的方法,它引起了NPE。
您应该测试列表是否为空,并返回您想要的内容(例如,空字符串),如果是这样的话:
if (nList.getLength() == 0) {
return "";
}
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
答案 2 :(得分:0)
替换
System.out.println("First Name : " + getTagValue("firstname", eElement));
通过
System.out.println("First Name : " + getTagValue("firstname", eElement) == null?"":getTagValue("firstname", eElement));
其他标签也一样。
答案 3 :(得分: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 "";
}