我是Java / XML的新手,我正在尝试解析XML文档。我可以浏览节点并获取我感兴趣的第一个节点的元素,但是一旦尝试遍历第二个节点,我就会得到一个空指针异常。
这是XML的示例。
<?xml version="1.0" encoding="utf-8"?>
<ICD10CM.index>
<version>2019</version>
<title>ICD-10-CM INDEX TO DISEASES and INJURIES</title>
<letter>
<title>A</title>
<mainTerm>
<title>Aarskog's syndrome</title>
<code>Q87.1</code>
</mainTerm>
<mainTerm>
<title>Abandonment</title>
<see>Maltreatment</see>
</mainTerm>
<mainTerm>
<title>Abasia<nemod>(-astasia) (hysterical)</nemod></title>
<code>F44.4</code>
</mainTerm>
<mainTerm>
<title>Abderhalden-Kaufmann-Lignac syndrome<nemod>(cystinosis)</nemod></title>
<code>E72.04</code>
</mainTerm>
<mainTerm>
<title>Abdomen, abdominal</title>
<seeAlso>condition</seeAlso>
<term level="1">
<title>acute</title>
<code>R10.0</code>
</term>
<term level="1">
<title>angina</title>
<code>K55.1</code>
</term>
<term level="1">
<title>muscle deficiency syndrome</title>
<code>Q79.4</code>
</term>
</mainTerm>
<mainTerm>
<title>Abdominalgia</title>
<see>Pain, abdominal</see>
</mainTerm>
<mainTerm>
<title>Abduction contracture, hip or other joint</title>
<see>Contraction, joint</see>
</mainTerm>
<mainTerm>
<title>Azygos</title>
<term level="1">
<title>continuation inferior vena cava</title>
<code>Q26.8</code>
</term>
<term level="1">
<title>lobe<nemod>(lung)</nemod></title>
<code>Q33.1</code>
</term>
</mainTerm>
</letter>
<letter>
<title>B</title>
<mainTerm>
<title>Baastrup's disease</title>
<see>Kissing spine</see>
</mainTerm>
<mainTerm>
<title>Babesiosis</title>
<code>B60.0</code>
</mainTerm>
<mainTerm>
<title>Babington's disease<nemod>(familial hemorrhagic telangiectasia)</nemod></title>
<code>I78.0</code>
</mainTerm>
<mainTerm>
<title>Babinski's syndrome</title>
<code>A52.79</code>
</mainTerm>
</letter>
<letter>
<title>Z</title>
<mainTerm>
<title>Zahorsky's syndrome<nemod>(herpangina)</nemod></title>
<code>B08.5</code>
</mainTerm>
<mainTerm>
<title>Zellweger's syndrome</title>
<code>Q87.89</code>
</mainTerm>
<mainTerm>
<title>Zenker's diverticulum<nemod>(esophagus)</nemod></title>
<code>K22.5</code>
</mainTerm>
<mainTerm>
<title>Ziehen-Oppenheim disease</title>
<code>G24.1</code>
</mainTerm>
<mainTerm>
<title>Zieve's syndrome</title>
<code>K70.0</code>
</mainTerm>
<mainTerm>
<title>Zika NOS</title>
<code>A92.5</code>
<term level="1">
<title>congenital</title>
<code>P35.4</code>
</term>
</mainTerm>
<mainTerm>
<title>Zinc</title>
<term level="1">
<title>deficiency, dietary</title>
<code>E60</code>
</term>
<term level="1">
<title>metabolism disorder</title>
<code>E83.2</code>
</term>
</mainTerm>
<mainTerm>
<title>Zollinger-Ellison syndrome</title>
<code>E16.4</code>
</mainTerm>
<mainTerm>
<title>Zona</title>
<see>Herpes, zoster</see>
</mainTerm>
<mainTerm>
<title>Zoophobia</title>
<code>F40.218</code>
</mainTerm>
<mainTerm>
<title>Zoster<nemod>(herpes)</nemod></title>
<see>Herpes, zoster</see>
</mainTerm>
<mainTerm>
<title>Zygomycosis</title>
<code>B46.9</code>
<term level="1">
<title>specified NEC</title>
<code>B46.8</code>
</term>
</mainTerm>
<mainTerm>
<title>Zymotic</title>
<see>condition</see>
</mainTerm>
</letter>
</ICD10CM.index>
然后输入我的代码:
try {
// Create Results File
File file = new File("ICD10-Parsed.txt");
FileWriter fileWriter = new FileWriter(file);
//Build XML Document and get from url
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse("h:/Application/API/icd10trial.xml");
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("letter");
System.out.println("----------------------------");
System.out.println("Number of nodes " + nList.getLength());
//Testing Area
for (int i =0; i<1 /*nList.getLength()*/;){
Node cNode = nList.item(i);
System.out.println("\nCurrent Element: " + cNode.getNodeName());
if (cNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) cNode;
System.out.println("Letter: " + eElement.getElementsByTagName("mainTerm").item(i).getTextContent());
NodeList lList = eElement.getElementsByTagName("mainTerm");
for (int x =0; x<lList.getLength();){
Node tNode = lList.item(x);
if (cNode.getNodeType() == Node.ELEMENT_NODE) {
String condition ="";
Element tElement = (Element) tNode;
NodeList tList = tElement.getElementsByTagName("title");
//Get Condition
NodeList elements = tElement.getElementsByTagName("title");
Node item = elements.item(i);
condition = item.getTextContent();
//condition = tElement.getElementsByTagName("title").item(i).getTextContent();
System.out.println("Condition: " + condition);
fileWriter.write(condition +";");
}
x++;
}
}
System.out.println("----------------------------");
i++;
}
fileWriter.flush();
fileWriter.close();
}catch (Exception e) {
e.printStackTrace();
}
如果我使用(int = 0; int <1;),我将得到以下输出,效果很好:
Root element :ICD10CM.index
----------------------------
Number of nodes 3
Current Element: letter
Letter:
Aarskog's syndrome
Q87.1
Condition: Aarskog's syndrome
Condition: Abandonment
Condition: Abasia(-astasia) (hysterical)
Condition: Abderhalden-Kaufmann-Lignac syndrome(cystinosis)
Condition: Abdomen, abdominal
Condition: Abdominalgia
Condition: Abduction contracture, hip or other joint
Condition: Azygos
----------------------------
BUILD SUCCESSFUL (total time: 0 seconds)
但是当我更改为(int = 0; int <2;)的那一刻,我得到以下输出:
run:
Root element :ICD10CM.index
----------------------------
Number of nodes 3
Current Element: letter
Letter:
Aarskog's syndrome
Q87.1
java.lang.NullPointerException
Condition: Aarskog's syndrome
Condition: Abandonment
Condition: Abasia(-astasia) (hysterical)
Condition: Abderhalden-Kaufmann-Lignac syndrome(cystinosis)
Condition: Abdomen, abdominal
Condition: Abdominalgia
Condition: Abduction contracture, hip or other joint
Condition: Azygos
----------------------------
at icd10parse.ICD10Parse.main(Current Element: letter
ICD10Parse.java:71)
Letter:
Babesiosis
B60.0
BUILD SUCCESSFUL (total time: 0 seconds)
我想念什么?
谢谢!
答案 0 :(得分:3)
通常,要在一行中找到一个空指针,请将该行中的所有调用更改为自己的行。
所以这个condition = tElement.getElementsByTagName("title").item(i).getTextContent();
会变成这样:
NodeList elements = tElement.getElementsByTagName("title");
Node item = elements.item(i);
condition = item.getTextContent();
这样,您将看到导致异常的各个部分中的哪个,并且可以缩小搜索范围。正如here中Colm所述,您的错误很可能是在这种情况下在i
中使用.item(i)
。因此,此答案的目的是帮助您下次解决该问题。
答案 1 :(得分:2)
可能的原因。您正在使用索引i引用“标题”元素。但是索引i是您用来遍历“字母”元素的内容。如果您只想要第一个标题,请在以下行中将i替换为1:
condition = tElement.getElementsByTagName("title").item(i).getTextContent();
答案 2 :(得分:0)
弄清楚了。我需要另一个变量用于子节点。现在效果很好。 这是工作代码:
Node cNode = nList.item(0);
int v = 0;
for (int a =0; a<nList.getLength();){
cNode = nList.item(a);
System.out.println("\nCurrent Element: " + cNode.getNodeName());
Element aElement = (Element) cNode;
System.out.println("Current Letter: " + aElement.getElementsByTagName("title").item(v).getTextContent());
//For each Node, get sub-nodes
NodeList lList = aElement.getElementsByTagName("mainTerm");
for (int x =0; x<lList.getLength();){
Node tNode = lList.item(x);
if (cNode.getNodeType() == Node.ELEMENT_NODE) {
String condition ="";
Element tElement = (Element) tNode;
NodeList tList = tElement.getElementsByTagName("title");
//Get Condition
NodeList elements = tElement.getElementsByTagName("title");
Node item = elements.item(v);
condition = item.getTextContent();
//condition = tElement.getElementsByTagName("title").item(i).getTextContent();
System.out.println("Condition: " + condition);
fileWriter.write(condition +";");
}
x++;
}
a++;
}
作为旁注,我可以不用将v变量设置为0。