繁体   English   中英

空指针遍历xml

[英]nullpointer iterate through xml

我在遍历循环以获取值时收到nullpointer错误。 我有一个如下所示的xml文件,我想获取driverid,vehicleid,act的值。 但是Java会抛出nullpointer,我是个初学者,不要粗鲁。 先感谢您。

    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示例会有所帮助,但是不,抱歉。 所以这是完整的xml文件

 <?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错误。 它应该是

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

这将为您提供所有route元素的列表,然后您应该可以进行迭代。

您的包含一个solutions元素,而该元素根本没有出现在xml中。 另外,如果要检查包含的数据,则确实需要包括text()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM