【问题标题】:Read xml file with java to access every element使用 java 读取 xml 文件以访问每个元素
【发布时间】:2018-02-27 10:07:59
【问题描述】:

我有一个如下所示的 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 标记内的值。请帮助我现在有点困惑

【问题讨论】:

  • 第 46 行是哪一行?
  • @f1sh 没有重复的伙伴,我想读取 startLocation 标签内的值
  • returnTodepot 上的错字...
  • @MattieuKevin 是的,这是重复的。如果您对重复的建议付出了一些努力,您会发现eElement.getElementsByTagName("returnTodepot")null。这会指出你错字的方向。

标签: java xml xmlreader


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    System.out.println(eElement.getTextContent());
    
    
    <returnToDepot>true</returnToDepot> 
    

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

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

    【讨论】:

    • 谢谢你现在它正在工作,关于如何检索 startLocation 标签内的值的任何想法?
    猜你喜欢
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多