nullpointer iterate through xml

2019-08-28 21:56发布

问题:

I'm getting nullpointer error when iterating through loop to get the values. I have a xml file like below, i would like to get the values of driverid, vehicleid, act. But java is throwing nullpointer,i'm a beginner plaese don't be rude. Thank you in advance.

    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());
         }
        }
    }

}

The problem seems to occur at line 41, which is

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

(#EDITED)Actually the xml provided above was not the original i thought that posting a smaller sample of the xml would be helpful but no, sorry for the inconvenient. So here is the complete xml file

<?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>

回答1:

Your xpath is wrong. It should be

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

which gives you a list of all route elements you should then be able to iterate.

Yours contained a solutions element which just didn't occur in the xml at all. Also, you do need to include text() if you want to check the contained data.

For further reference, it's a good idea to use an online xpath tester to be sure you retrieve the data you want.