From the xml, I need to get name and area for each node.
I know the sample xpath like,
XMLDependency/@name|XMLDependency/@area, which gives comma separated values,
Output:
Name1, JKL, Name2, MNO
XML:
<DocBuild name="ABCD">
<XMLDependency name="Name1" product="Product ABC" area="JKL" />
<XMLDependency name="Name2" product="Product DEF" area="MNO" />
<XMLDependency name="Name3" product="Product GHI" area="PQR" />
</DocBuild>
Doubts:
How to handle if theareais not mandatory for node(value may present or absent).- Can we insert any static text between
nameandareaso that we can parse the String using utilities ? - Any Xpath expression apart from above to get the values to map ?(may be using adapter)
Please help !
[EDIT] :
This is the Java program I started to use.
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.compile("//XMLDependency/@name|//XMLDependency/@area")
.evaluate(element, XPathConstants.NODESET);
List<String> nodes = new ArrayList<String>();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
nodes.add(node.getNodeValue());
}
System.out.println(nodes);
Handling by using node type and node value gets me first point solved.
//XMLDependency/@name|//XMLDependency/@areawould. Why don't you test what happens if anareaattribute is absent? Not sure what you mean by the next two "doubts". Can you show your Java code?Name1, JKL, Name2. From java side we cannot handle this. So, if we can get result likeName1:JKL, Name2:MNO, then even though if some node doesnt contain area, it results asName1:JKL, Name2:. The above result can be handled with care using utilities. But how to introduce:betweenName1andJKLis my second doubt. Third one I mentioned about any other ways to get data using any other XPATH expression.<DocBuild>element? And what are you using to insert commas between the results? That isn't coming from your XPath expression alone. Whatever answer we give will have to integrate with the stuff in your Java code that you haven't shown yet.