空指针遍历xml [英] nullpointer iterate through xml

查看:39
本文介绍了空指针遍历xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在遍历循环以获取值时遇到空指针错误.我有一个像下面这样的 xml 文件,我想获取 driverid、vehicleid、act 的值.但是java正在抛出空指针,我是初学者请不要粗鲁.提前致谢.

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

}

问题好像出现在第41行,也就是

The problem seems to occur at line 41, which is

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

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

(#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>

推荐答案

您的 xpath 是错误的.应该是

Your xpath is wrong. It should be

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

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

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

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

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.

为了进一步参考,最好使用 在线 xpath 测试器 以确保检索到所需的数据.

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

这篇关于空指针遍历xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆