[英]DOM - Missing Element in XML Document
The XML file contains Employee (with empID, empName, empCode). XML文件包含Employee(具有empID,empName,empCode)。 In some situation empCode is missing.
在某些情况下,缺少empCode。
<Employee><Detail><empID>1</empID><empName>Abhi</empName><empCode>One</empCode>
</Detail>
<Detail><empID>2</empID><empName>Amit</empName>
</Detail>
</Employee>
I am getting the Null pointer Exception while calling the getTagValue() method for "empCode" as there is no tag available with the name in XML. 我在为“ empCode”调用getTagValue()方法时遇到Null指针异常,因为XML中的名称没有可用的标签。
Java Code : Java代码:
try { File xmlFile = new File("New.xml"); 尝试{File xmlFile = new File(“ New.xml”);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Detail");
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nNode;
EmpDetail empInfo = new EmpDetail();
empInfo.SetEmpID(getTagValue("empID", eElement));
empInfo.SetEmpName(getTagValue("empName",eElement));
empInfo.SetEmpCode(getTagValue("empCode",eElement));
DBConnector.SaveinDB(empInfo);
}
}
}
catch (Exception e)
{
System.out.println("Error: ");
e.printStackTrace();
}
}
private static String getTagValue(String sTag, Element eElement)
{
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
if(nValue == null)
return null;
return insertEscapeSequance(nValue.getNodeValue());
}
private static String insertEscapeSequance(String str)
{
String returnstr = "";
String[] strarr = str.split("'");
returnstr = strarr[0];
for(int i=1;i<strarr.length;i++)
{
returnstr = returnstr + "\\'" + strarr[i];
}
return returnstr;
}
Now I want to save the XML data into sql like this : 现在,我想像这样将XML数据保存到sql中:
1 Abhi One 2 Amit null 1 Abhi One 2 Amit空
I tried so many links but not success. 我尝试了很多链接,但没有成功。 Can someone please help me
有人可以帮帮我吗
If this is possible that there is no such value, then simply handle it like that: 如果有可能没有这样的值,则只需像这样处理它:
if(getTagValue("empCode",eElement) != null){
empInfo.SetEmpCode(getTagValue("empCode",eElement));
}
Regarding to adding them into SQL, do the same check while creating your statement. 关于将它们添加到SQL中,在创建语句时进行相同的检查。 As SQL NULL type exists
由于存在SQL NULL类型
The problem is caused by eElement.getElementsByTagName(sTag).item(0)
. 该问题是由
eElement.getElementsByTagName(sTag).item(0)
。 The statement returns the first node specified by sTag
. 该语句返回
sTag
指定的第一个节点。 In the case of empCode
, null
is returned, and calling getChildNodes()
will raise a null pointer exception. 对于
empCode
,将返回null
,并且调用getChildNodes()
将引发null指针异常。
Try: 尝试:
Node node = eElement.getElementsByTagName(sTag).item(0);
if (node != null) {
Node nValue = node.getChildNodes().item(0);
if (nValue != null) {
return insertEscapeSequance(nValue.getNodeValue());
}
}
return null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.