如何使用java从XML获取Node值

时间:2016-10-25 12:21:52

标签: java xml

我正在尝试从XML中查找节点值并需要存储在数据库中。但首先尝试在控制台中打印节点值。

XML内容是:

<ServiceRouteInfo xmlns="http:..." xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <ExtraDimensions xmlns:a="http://..">...</ExtraDimensions>
  <FreightDimensions xmlns:a="http:..." i:nil="true"/>
  <MapParams>...</MapParams>
  <MiniCruise xmlns:a="http:..." i:nil="true"/>
  <Operators>
    <OperatorList>
      <Code>NL</Code>
      <CompanyName>No</CompanyName>
      <DepartCode>AB</DepartCode>
      <DestCode>KI</DestCode>
    </OperatorList>
  </Operators>
</ServiceRouteInfo>

代码:

public void run(String... arg0) throws Exception {
    try {
        FileInputStream file = new FileInputStream(new File("D:/Ferries_RouteXml/Aberdeen - Kirkwall.xml"));

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        DocumentBuilder builder =  builderFactory.newDocumentBuilder();

        Document xmlDocument = builder.parse(file);

        XPath xPath =  XPathFactory.newInstance().newXPath();

        System.out.println("*************************");
        String expression = "/ServiceRouteInfo /Operators/OperatorList";
        //String expression = "ServiceRouteInfo[CoCode]";
        System.out.println(expression);
        NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.println("CoCode :  " + nodeList.item(i).getFirstChild().getNodeValue());

        }           
        System.out.println("*************************");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }   


}

我只需要在控制台中打印Code,DepartCode,DestCode值。请帮帮我..

2 个答案:

答案 0 :(得分:0)

如果你使用类似的东西,你可以逐个直接访问这些值:

String expression = "/ServiceRouteInfo /Operators/OperatorList/Code/text()";
String value = xPath.compile(expression).evaluate(xmlDocument);

答案 1 :(得分:0)

我稍微修改了你的代码,这里是:

public void run(String... arg0) throws Exception {
    try {
        FileInputStream file = new FileInputStream(new File("D:/Ferries_RouteXml/Aberdeen - Kirkwall.xml"));


        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        DocumentBuilder builder =  builderFactory.newDocumentBuilder();

        Document xmlDocument = builder.parse(file);

        XPath xPath =  XPathFactory.newInstance().newXPath();

        System.out.println("*************************");
        String expression = "/ServiceRouteInfo/Operators/OperatorList/*";
        System.out.println(expression);
        NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
        String[] descriptions = {"Code", "CompanyName", "DepartCode", "DestCode"};
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node item = nodeList.item(i);
            System.out.println(descriptions[i]+":  " + item.getFirstChild().getNodeValue());

        }
        System.out.println("*************************");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
}

注意行:String expression = "/ServiceRouteInfo/Operators/OperatorList/*";