读取XML节点Java时出现NullPointerException
我试图读取一些XML节点并将它们存储到Java对象中。我能读懂物体的一些元素,但我不能读懂其他元素,我不知道为什么。以下是XML的一部分的外观:读取XML节点Java时出现NullPointerException,java,xml,Java,Xml,我试图读取一些XML节点并将它们存储到Java对象中。我能读懂物体的一些元素,但我不能读懂其他元素,我不知道为什么。以下是XML的一部分的外观: <problems> <problem id = "1"> <quest>22-16:4</quest> <result>18</result> <chapter_id>1</chapter_id> <
<problems>
<problem id = "1">
<quest>22-16:4</quest>
<result>18</result>
<chapter_id>1</chapter_id>
<type>text</type>
</problem>
<problem id = "2">
<quest>16+2*12</quest>
<result>30</result>
<chapter_id>1</chapter_id>
<type>text</type>
</problem>
<problem id = "3">
<quest>72:2-18</quest>
<result>18</result>
<chapter_id>1</chapter_id>
<type>text</type>
</problem>
<problem id = "4">
<quest>12*4-15:5</quest>
<result>45</result>
<chapter_id>1</chapter_id>
<type>text</type>
</problem>
</problems>
你知道问题出在哪里了吗?谢谢 首先,您能给出完整模块的一个片段吗?错误显示在第57行,但没有足够的行显示实际崩溃的位置,可能是您无法正确访问,或者您正在从Xml读取EOF。 或者,至少,破坏了它崩溃的确切路线
帮助我们帮助您:)首先,您能给出完整模块的一个片段吗?错误显示在第57行,但没有足够的行显示实际崩溃的位置,可能是您无法正确访问,或者您正在从Xml读取EOF。 或者,至少,破坏了它崩溃的确切路线
帮助我们帮助您:)如果您需要,可以尝试下面的方法:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("C:/sample.xml");
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
if (doc.hasChildNodes()) {
printNote(doc.getChildNodes());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void printNote(NodeList nodeList) {
System.out.println("nodeList.getLength() = "+nodeList.getLength());
for (int count = 0; count < nodeList.getLength(); count++) {
Node tempNode = nodeList.item(count);
// make sure it's element node.
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
// get node name and value
System.out.println("Node Name =" + tempNode.getNodeName());
// System.out.println("Node Value =" + tempNode.getTextContent());
if (tempNode.hasAttributes()) {
// get attributes names and values
NamedNodeMap nodeMap = tempNode.getAttributes();
for (int i = 0; i < nodeMap.getLength(); i++) {
Node node = nodeMap.item(i);
System.out.println("\t attr name : " + node.getNodeName());
System.out.println("\t attr value : " + node.getNodeValue());
}
}
else if (tempNode.hasChildNodes()) {
// loop again if has child nodes
printNote(tempNode.getChildNodes());
}
}
}
}
}
导入java.io.File;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入org.w3c.dom.Document;
导入org.w3c.dom.NamedNodeMap;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
公共类ReadXMLFile{
公共静态void main(字符串argv[]){
试一试{
File fXmlFile=新文件(“C:/sample.xml”);
DocumentBuilder dBuilder=DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
if(doc.hasChildNodes()){
printNote(doc.getChildNodes());
}
}捕获(例外e){
System.out.println(e.getMessage());
}
}
私有静态void打印注释(节点列表节点列表){
System.out.println(“nodeList.getLength()=”+nodeList.getLength());
对于(int count=0;count
如果需要,您可以尝试以下方法:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("C:/sample.xml");
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
if (doc.hasChildNodes()) {
printNote(doc.getChildNodes());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void printNote(NodeList nodeList) {
System.out.println("nodeList.getLength() = "+nodeList.getLength());
for (int count = 0; count < nodeList.getLength(); count++) {
Node tempNode = nodeList.item(count);
// make sure it's element node.
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
// get node name and value
System.out.println("Node Name =" + tempNode.getNodeName());
// System.out.println("Node Value =" + tempNode.getTextContent());
if (tempNode.hasAttributes()) {
// get attributes names and values
NamedNodeMap nodeMap = tempNode.getAttributes();
for (int i = 0; i < nodeMap.getLength(); i++) {
Node node = nodeMap.item(i);
System.out.println("\t attr name : " + node.getNodeName());
System.out.println("\t attr value : " + node.getNodeValue());
}
}
else if (tempNode.hasChildNodes()) {
// loop again if has child nodes
printNote(tempNode.getChildNodes());
}
}
}
}
}
导入java.io.File;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入org.w3c.dom.Document;
导入org.w3c.dom.NamedNodeMap;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
公共类ReadXMLFile{
公共静态void main(字符串argv[]){
试一试{
File fXmlFile=新文件(“C:/sample.xml”);
DocumentBuilder dBuilder=DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
if(doc.hasChildNodes()){
printNote(doc.getChildNodes());
}
}捕获(例外e){
System.out.println(e.getMessage());
}
}
私有静态void打印注释(节点列表节点列表){
System.out.println(“nodeList.getLength()=”+nodeList.getLength());
对于(int count=0;count
damn,我从未使用过代码段,所以我现在不知道如何创建代码段,但在我查找之前,给出错误的行是:p.setType(eeelement.getElementsByTagName(“type”).item(0.getTextContent());另外,如果我只是注释那一行,而不读取type元素,它不会给出错误并且工作正常。您知道您正在读取哪个标记吗?另外,我也不是很确定,但是“type”不是保留字吗?我更改了标签的名称,以确保它不是保留字,并且仍然不起作用。这很奇怪,因为其他所有的东西都在工作,其余的标签都被正确地读取了,接下来要做的是确定它重复了多少次。有时getElementsByTagName的工作原理很奇怪(不要
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("C:/sample.xml");
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
if (doc.hasChildNodes()) {
printNote(doc.getChildNodes());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static void printNote(NodeList nodeList) {
System.out.println("nodeList.getLength() = "+nodeList.getLength());
for (int count = 0; count < nodeList.getLength(); count++) {
Node tempNode = nodeList.item(count);
// make sure it's element node.
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
// get node name and value
System.out.println("Node Name =" + tempNode.getNodeName());
// System.out.println("Node Value =" + tempNode.getTextContent());
if (tempNode.hasAttributes()) {
// get attributes names and values
NamedNodeMap nodeMap = tempNode.getAttributes();
for (int i = 0; i < nodeMap.getLength(); i++) {
Node node = nodeMap.item(i);
System.out.println("\t attr name : " + node.getNodeName());
System.out.println("\t attr value : " + node.getNodeValue());
}
}
else if (tempNode.hasChildNodes()) {
// loop again if has child nodes
printNote(tempNode.getChildNodes());
}
}
}
}
}