2016-10-25 2 views
0

Я пытаюсь найти значение узла из XML и хранить его в базе данных. Но сначала попытаемся напечатать значение узла в консоли.Как получить значение узла из XML с помощью java

XML Content являются:

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


} 

Мне нужен только код, DepartCode, значение DestCode для печати в консоли. Просьба помочь мне ..

+0

И в чем ваш вопрос? Ваша конкретная проблема? – vanje

ответ

0

вы можете получить быстрый доступ к этим числам один за другим, если и использовать что-то вроде этого:

String expression = "/ServiceRouteInfo /Operators/OperatorList/Code/text()"; 
String value = xPath.compile(expression).evaluate(xmlDocument); 
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/*";