提问人:HelpIsNeeded 提问时间:4/25/2022 更新时间:4/25/2022 访问量:516
从 XML 文件获取数据时出现问题
Problem with acquiring data from XML file
问:
我尝试过使用 DOM、Xpath,目前我只成功地使用流来遍历每一行并以这种方式获取数据。显然,我的 shelfNodeLists.getAttributes.getNamedItem 会产生它,我不明白它为什么要这样做。在任何当前论坛上都找不到任何相关信息。
我真的在努力让 DOM 工作,因为我有大量的 XML 文件要迭代,而且我听说它对大量数据很有效。
这是我得到的 NPE。
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.w3c.dom.Node.getTextContent()" because the return value of "org.w3c.dom.NamedNodeMap.getNamedItem(String)" is null
at ReadXmlDomParser.main(ReadXmlDomParser.java:75)
下面是产生异常的代码:
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
public class ReadXmlDomParser {
private static final String FILENAME = "src/main/resources/TestXML.xml";
public static void main(String[] args) {
// Instantiate the Factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
// process XML
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
// parse XML file
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(FILENAME));
doc.getDocumentElement().normalize();
System.out.println("Root Element :" + doc.getDocumentElement().getNodeName());
System.out.println("------");
// get <Shelf>
NodeList list = doc.getElementsByTagName("Shelf");
for (int temp = 0; temp < list.getLength(); temp++) {
Node node = list.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
// get shelf's attribute
String id = element.getAttribute("ShelfID");
// get text
String positionText = element.getElementsByTagName("Position").item(0).getTextContent();
String dangerLevelText = element.getElementsByTagName("DangerLevel").item(0).getTextContent();
String coffeeText = element.getElementsByTagName("Coffee").item(0).getTextContent();
NodeList shelfNodeList = element.getElementsByTagName("Position");
NodeList shelfNodeList1 = element.getElementsByTagName("DangerLevel");
NodeList shelfNodeList2 = element.getElementsByTagName("Coffee");
//get attributes
String position = shelfNodeList.item(0).getAttributes().getNamedItem("Position").getTextContent();
String dangerLevel = shelfNodeList1.item(0).getAttributes().getNamedItem("DangerLevel").getTextContent();
String coffee = shelfNodeList2.item(0).getAttributes().getNamedItem("Coffee").getTextContent();
System.out.println("Current Element :" + node.getNodeName());
System.out.println("Player Id : " + id);
System.out.printf("Position : %,.2f [%s]%n%n", Float.parseFloat(positionText), position);
System.out.printf("DangerLevel [Stat] : %,.2f [%s]%n%n", Float.parseFloat(dangerLevelText), dangerLevel);
System.out.printf("Coffee [Stat] : %,.2f [%s]%n%n", Float.parseFloat(coffeeText), coffee);
}
}
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
}
这是我尝试从中提取数据的XML文件:
<Section>
<Shelf ShelfID="p60772">
<Position BioChemicalID="Bio1523">1</Position>
<Position BioChemicalID="Bio9612">4</Position>
<DangerLevel scale="1-10"> 6</DangerLevel>
<DangerLevel scale="1-10"> 2</DangerLevel>
<Coffee java="Yes"> 100%</Coffee>
</Shelf>
</Section>
答: 暂无答案
评论