1

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 the area is not mandatory for node(value may present or absent).
  • Can we insert any static text between name and area so 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.

5
  • 1
    Your path expression does not yield any results, //XMLDependency/@name|//XMLDependency/@area would. Why don't you test what happens if an area attribute is absent? Not sure what you mean by the next two "doubts". Can you show your Java code? Commented Sep 23, 2014 at 7:14
  • Im evaluating the XPATH expression from IntelliJ. that expression yields result as shown in question. If area is not present then it is resulting as Name1, JKL, Name2. From java side we cannot handle this. So, if we can get result like Name1:JKL, Name2:MNO, then even though if some node doesnt contain area, it results as Name1:JKL, Name2:. The above result can be handled with care using utilities. But how to introduce : between Name1 and JKL is my second doubt. Third one I mentioned about any other ways to get data using any other XPATH expression. Commented Sep 23, 2014 at 9:10
  • Since you're using code in the environment (some Java in IntelliJ) to set a context that changes the results of the XPath expression, you need to show us that context. Apparently you're setting the context node to the <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. Commented Sep 23, 2014 at 15:09
  • @MathiasMüller, Apologies for not making it clearer, added java program. Im deceived by the output that list prints(that is adding comma between the list items), thinking that it might be because of Xpath expression. Debugged it. got to know the way using node name and value. Commented Sep 23, 2014 at 16:10
  • @LarsH, Unable to mark two users for same comment. Please have a look on above one. First one Im marking as invalid. Still my second one remains as it is. Commented Sep 23, 2014 at 16:11

1 Answer 1

3

What you're trying to do cannot be accomplished with XPath alone. You need to iterate once for each XMLDependency and combine the values starting from there:

XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.compile("//XMLDependency")
                                    .evaluate(element, XPathConstants.NODESET);

List<String> nodes = new ArrayList<String>();

for (int i = 0; i < nodeList.getLength(); i++) {
    NamedNodeMap att = nodeList.item(i).getAttributes();

    Node name = att.getNamedItem("name");
    Node area = att.getNamedItem("area");

    nodes.add(name.getNodeValue() + ":" + 
              (area != null ? area.getNodeValue() : ""));
}

System.out.println(nodes);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.