java - 使用java读取xml文件来访问每个元素

标签 java xml xmlreader

我有一个 XML 文件,如下所示。我已经创建了一个简单的程序来读取该文件并在控制台上打印。我面临的问题是它给了我

java.lang.NullPointerException
    at xmlreader.main(xmlreader.java:46)

而且它也没有读取某些值。

 <problemType>
      <fleetSize>FINITE</fleetSize>
 </problemType>
 <vehicles>
      <vehicle>
           <id>1_1</id>
           <typeId>type_1</typeId>
           <startLocation>
                <id>[x=-20.2675][y=57.4797]</id>
                <coord x="-20.2675" y="57.4797"/>
           </startLocation>
           <endLocation>
                <id>[x=-20.2675][y=57.4797]</id>
                <coord x="-20.2675" y="57.4797"/>
           </endLocation>
           <timeSchedule>
                <start>0.0</start>
                <end>1.7976931348623157E308</end>
           </timeSchedule>
           <returnToDepot>true</returnToDepot>
      </vehicle>
      <vehicle>
      </vehicles>

我创建了一段java代码来读取这个文件

    import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class xmlreader {

   public static void main(String argv[]) {

    try {

    File fXmlFile = new File("C:/Users/HP/Desktop/solution.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    //optional, but recommended
    //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("vehicles");

    System.out.println("----------------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement = (Element) nNode;

            System.out.println("id : " + eElement.getAttribute("id"));
            System.out.println("typeId : " + eElement.getElementsByTagName("typeId").item(0).getTextContent());
            System.out.println("startLocation : " + eElement.getElementsByTagName("startLocation").item(0).getTextContent());
            System.out.println("endLocation : " + eElement.getElementsByTagName("endLocation").item(0).getTextContent());
            System.out.println("timeSchedule : " + eElement.getElementsByTagName("timeSchedule").item(0).getTextContent());
                        System.out.println("returnTodepot : " + eElement.getElementsByTagName("returnTodepot").item(0).getTextContent());
        }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }}

当我尝试运行该程序时,它给了我错误“java.lang.NullPointerException”,而且它似乎没有获取 startLocation 标记内的值。请帮助我现在有点困惑

最佳答案

由于拼写错误,您得到了空指针。

你想要一个元素:returnTodepot
而元素名称是:returnToDepot

关于java - 使用java读取xml文件来访问每个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49006042/

相关文章:

java - 如何在java中打印带括号的中序AVL树遍历

security - 在哪里可以下载适用于任何浏览器的 JRE 7u5 插件?

xml - 当我重复项目时,如何在 grxml 文件中使用语义标记?

android:如何从 strings.xml 中的字符串数组中获取项目的属性

C# 解析以下 XML,不确定如何

c# - 调查 XMLReader 对象

java - 自动构建maven插件

java - Spark Streaming 中的 Kafka 消费者

java - 似乎无法在 android 中解析 xml 文件而不出现 saxparser 意外 token 错误

c# - IgnoreWhiteSpace 不忽略 xml 字符串开头的空格