我有这个下面的XML元素,但我有一个问题需要解析它。我正在使用DOM解析器从每个维度中提取元素数据,这些维度可以具有一个或多个聚合级别,并且可以具有多个属性。
<Dimensions>
<Dimension name="Time" refD="ID-T">
<Aggregation_level>
<level> second </level>
<level> Minute </level>
<level> Hour </level>
<level> Day </level>
<level> Month </level>
<level> Year </level>
</Aggregation_level>
</Dimension>
<Dimension name="Place" refD="ID-PL">
<aggregation_level>
<level> Place_type </level>
</aggregation_level>
<aggregation_level>
<level> Country-code </level>
<level_attribute>country</level_attribute>
</aggregation_level>
<Attributes>
<Attribute>Full_Name</Attribute>
<Attribute>Name</Attribute>
<Attribute>URL_PL</Attribute>
</Attributes>
</Dimension>
</Dimensions>
这是我的代码:
NodeList nodes = document.getElementsByTagName("Dimensions");
// extract liste des dimensions
Element element = (Element) nodes.item(0);
NodeList dimensionList = element.getElementsByTagName("Dimension");
for (int i = 0; i < dimensionList.getLength(); i++) {
Element dimensionElement = (Element) dimensionList.item(i);
System.out.println(dimensionElement.getAttributes().getNamedItem("name").getNodeValue());
NodeList levels = dimensionElement.getElementsByTagName("Aggregation_level");
NodeList attributs = dimensionElement.getElementsByTagName("Attributes");
Element level = (Element)levels.item(0);
Element attribute = (Element) attributs.item(0);
System.out.println("verification1");
while(level.getNodeType() == Node.ELEMENT_NODE && level.hasChildNodes()) {
for(int j=0; j< level.getChildNodes().getLength(); j++){
System.out.println(level.getElementsByTagName("level").item(j).getTextContent());
}
}
while(attribute.getNodeType() == Node.ELEMENT_NODE && attribute.hasChildNodes()) {
for(int j1=0; j1< attribute.getChildNodes().getLength(); j1++){
System.out.println(attribute.getElementsByTagName("Attribute").item(j1).getTextContent());
}
}}
但我收到了这个结果:
时间第二分钟小时日月线程中的异常 &#34;主&#34; java.lang.NullPointerException at ParsingJson.main(ParsingJson.java:45)