javaxmlxpathjexcelapi

xPath expression in Java


I have the following xml file:

<?xml version="1.0" encoding="UTF-8"?>
<include template="template.xml">
      <param name="view" value="exampleValue"/>
      <param name="table" value="exampleValue"/>
      <param name="title" value="exampleValue"/>
      <param name="addTitle" value="exampleValue"/>
      <param name="permission" value="exampleValue"/>
      <param name="icon" value="exampleValue"/>
    </include> 

Now I want to check with xPath in my Java project if there is a <include>-node with a attribute called template and if the child nodes contain a name-attribute and the accompanying value.

I tried this:

String expression ="//include[@template]";                                                                          //Expression;
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);


for (int i = 0; i < nodeList.getLength(); i++) 
{
  String match = nodeList.item(i).getAttributes().getNamedItem("value").toString();
  String match2 = nodeList.item(i).getAttributes().getNamedItem("template").toString();
  System.out.println(match);
  System.out.println(match2);
}

Solution

  • try this /include[@template]/*[@name][@value]

    this will give you the nodes which contains name and value under include element/node.