I'm trying to find node value from XML and need to store in Database . But first trying to print the node value in console.
XML Content are:
<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>
Code:
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();
}
}
I need only Code,DepartCode,DestCode value to print in console. pls help me..
you can directly access these values one by one if u use something like that:
String expression = "/ServiceRouteInfo /Operators/OperatorList/Code/text()";
String value = xPath.compile(expression).evaluate(xmlDocument);
I modified your code a little bit and here it is:
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();
}
}
Note line: String expression = "/ServiceRouteInfo/Operators/OperatorList/*";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.