nullpointer遍历xml

时间:2018-03-02 06:20:55

标签: java xml nullpointerexception

我在迭代循环以获取值时得到nullpointer错误。我有一个像下面的xml文件,我想得到driverid,vehicleid,act的值。但是java正在抛出无效指针,我是一个初学者,并不粗鲁。提前谢谢。

    package xpath;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

public class Xpath {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = Factory.newDocumentBuilder();
        Document doc = builder.parse("C:/Users/HP/Desktop/solution.xml");

        //creating an XPathFactory:
        XPathFactory factory = XPathFactory.newInstance();
        //using this factory to create an XPath object: 
        XPath xpath = factory.newXPath();

        // XPath Query for showing all nodes value
        XPathExpression expr = xpath.compile("problem/solutions/solution[cost='505.9208295302417']/routes/route");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;

        for (int i = 0; i < nodes.getLength(); i++) {

            System.out.println(nodes.getLength());
            ;
            Element el = (Element) nodes.item(i);

            if (el.getNodeType() == Node.ELEMENT_NODE) {
                System.out.println("driverid:" + el.getElementsByTagName("driverId").item(i).getTextContent());

            System.out.println("vehicleId:" + el.getElementsByTagName("vehicleId").item(i).getTextContent());
            System.out.println("Citizen:" + el.getElementsByTagName("act").item(i).getTextContent());
            System.out.println("Element currently in: " + el.getNodeName());
         }
        }
    }

}

问题似乎发生在第41行,即

System.out.println("driverid:" + el.getElementsByTagName("driverId").item(i).getTextContent());

(#EDITED)实际上上面提供的xml不是原始的我认为发布一个较小的xml样本会有所帮助,但是没有,抱歉不方便。所以这是完整的xml文件

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<problem xmlns="http://www.w3schools.com"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com vrp_xml_schema.xsd">
     <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>
               <id>1_2</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>
               <id>2_1</id>
               <typeId>type_2</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>
               <id>3_1</id>
               <typeId>type_3</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>
     </vehicles>
     <vehicleTypes>
          <type>
               <id>type_1</id>
               <capacity-dimensions>
                    <dimension index="0">120</dimension>
               </capacity-dimensions>
               <costs>
                    <fixed>0.0</fixed>
                    <distance>1.0</distance>
                    <time>0.0</time>
                    <service>0.0</service>
                    <wait>0.0</wait>
               </costs>
          </type>
          <type>
               <id>type_2</id>
               <capacity-dimensions>
                    <dimension index="0">160</dimension>
               </capacity-dimensions>
               <costs>
                    <fixed>0.0</fixed>
                    <distance>1.1</distance>
                    <time>0.0</time>
                    <service>0.0</service>
                    <wait>0.0</wait>
               </costs>
          </type>
          <type>
               <id>type_3</id>
               <capacity-dimensions>
                    <dimension index="0">300</dimension>
               </capacity-dimensions>
               <costs>
                    <fixed>0.0</fixed>
                    <distance>1.3</distance>
                    <time>0.0</time>
                    <service>0.0</service>
                    <wait>0.0</wait>
               </costs>
          </type>
     </vehicleTypes>
     <services>
          <service id="5 " type="service">
               <location>
                    <id>[x=57.49129867553711][y=-20.29949951171875]</id>
                    <coord x="57.49129867553711" y="-20.29949951171875"/>
               </location>
               <capacity-dimensions>
                    <dimension index="0">100</dimension>
               </capacity-dimensions>
               <duration>10.0</duration>
               <timeWindows>
                    <timeWindow>
                         <start>0.0</start>
                         <end>1.7976931348623157E308</end>
                    </timeWindow>
               </timeWindows>
          </service>
          <service id="2 " type="service">
               <location>
                    <id>[x=57.49300003051758][y=-20.298799514770508]</id>
                    <coord x="57.49300003051758" y="-20.298799514770508"/>
               </location>
               <capacity-dimensions>
                    <dimension index="0">20</dimension>
               </capacity-dimensions>
               <duration>10.0</duration>
               <timeWindows>
                    <timeWindow>
                         <start>0.0</start>
                         <end>1.7976931348623157E308</end>
                    </timeWindow>
               </timeWindows>
          </service>
          <service id="1 " type="service">
               <location>
                    <id>[x=57.49380111694336][y=-20.29840087890625]</id>
                    <coord x="57.49380111694336" y="-20.29840087890625"/>
               </location>
               <capacity-dimensions>
                    <dimension index="0">80</dimension>
               </capacity-dimensions>
               <duration>10.0</duration>
               <timeWindows>
                    <timeWindow>
                         <start>0.0</start>
                         <end>1.7976931348623157E308</end>
                    </timeWindow>
               </timeWindows>
          </service>
          <service id="3 " type="service">
               <location>
                    <id>[x=57.49290084838867][y=-20.298200607299805]</id>
                    <coord x="57.49290084838867" y="-20.298200607299805"/>
               </location>
               <capacity-dimensions>
                    <dimension index="0">80</dimension>
               </capacity-dimensions>
               <duration>10.0</duration>
               <timeWindows>
                    <timeWindow>
                         <start>0.0</start>
                         <end>1.7976931348623157E308</end>
                    </timeWindow>
               </timeWindows>
          </service>
          <service id="4 Jean Nicolas Yung" type="service">
               <location>
                    <id>[x=57.49209976196289][y=-20.298599243164062]</id>
                    <coord x="57.49209976196289" y="-20.298599243164062"/>
               </location>
               <capacity-dimensions>
                    <dimension index="0">100</dimension>
               </capacity-dimensions>
               <duration>10.0</duration>
               <timeWindows>
                    <timeWindow>
                         <start>0.0</start>
                         <end>1.7976931348623157E308</end>
                    </timeWindow>
               </timeWindows>
          </service>
     </services>
     <solutions>
          <solution>
               <cost>505.9214355631349</cost>
               <routes>
                    <route>
                         <driverId>noDriver</driverId>
                         <vehicleId>1_1</vehicleId>
                         <start>0.0</start>
                         <act type="service">
                              <serviceId>5 </serviceId>
                              <arrTime>109.9819741964403</arrTime>
                              <endTime>119.9819741964403</endTime>
                         </act>
                         <end>229.9639483928806</end>
                    </route>
                    <route>
                         <driverId>noDriver</driverId>
                         <vehicleId>3_1</vehicleId>
                         <start>0.0</start>
                         <act type="service">
                              <serviceId>2 </serviceId>
                              <arrTime>109.98268205388193</arrTime>
                              <endTime>119.98268205388193</endTime>
                         </act>
                         <act type="service">
                              <serviceId>1 </serviceId>
                              <arrTime>119.98357684436793</arrTime>
                              <endTime>129.98357684436792</endTime>
                         </act>
                         <act type="service">
                              <serviceId>3 </serviceId>
                              <arrTime>129.98449911991617</arrTime>
                              <endTime>139.98449911991617</endTime>
                         </act>
                         <act type="service">
                              <serviceId>4 Jean Nicolas Yung</serviceId>
                              <arrTime>139.98539391040217</arrTime>
                              <endTime>149.98539391040217</endTime>
                         </act>
                         <end>259.9672978232725</end>
                    </route>
               </routes>
          </solution>
          <solution>
               <cost>505.9208295302417</cost>
               <routes>
                    <route>
                         <driverId>noDriver</driverId>
                         <vehicleId>1_1</vehicleId>
                         <start>0.0</start>
                         <act type="service">
                              <serviceId>5 </serviceId>
                              <arrTime>109.9819741964403</arrTime>
                              <endTime>119.9819741964403</endTime>
                         </act>
                         <end>229.9639483928806</end>
                    </route>
                    <route>
                         <driverId>noDriver</driverId>
                         <vehicleId>3_1</vehicleId>
                         <start>0.0</start>
                         <act type="service">
                              <serviceId>4 Jean Nicolas Yung</serviceId>
                              <arrTime>109.98190391287031</arrTime>
                              <endTime>119.98190391287031</endTime>
                         </act>
                         <act type="service">
                              <serviceId>2 </serviceId>
                              <arrTime>119.98282618841856</arrTime>
                              <endTime>129.98282618841856</endTime>
                         </act>
                         <act type="service">
                              <serviceId>1 </serviceId>
                              <arrTime>129.98372097890456</arrTime>
                              <endTime>139.98372097890456</endTime>
                         </act>
                         <act type="service">
                              <serviceId>3 </serviceId>
                              <arrTime>139.9846432544528</arrTime>
                              <endTime>149.9846432544528</endTime>
                         </act>
                         <end>259.9668316441239</end>
                    </route>
               </routes>
          </solution>
     </solutions>
</problem>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你的xpath错了。它应该是

//solution[cost/text()='505.9208295302417']/routes/route

它为您提供了应该能够迭代的所有route元素的列表。

你的元素包含一个solutions元素,它根本没有在xml中出现。此外,如果要检查包含的数据,还需要包含text()

为了进一步参考,最好使用online xpath tester来确保检索所需的数据。