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

时间:2018-02-27 10:07:59

标签: 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标记内的值。请帮助我现在有点困惑

2 个答案:

答案 0 :(得分:1)

由于输入错误,您将获得nullpointer。

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

答案 1 :(得分:0)

调用returnToDepot的eElement.getTextContent()就足够了

System.out.println(eElement.getTextContent());


<returnToDepot>true</returnToDepot> 

returnToDepot没有任何子元素。 但是在你的代码中,试图访问索引,这不存在。

eElement.getElementsByTagName("returnTodepot").item(0)