0

Given the xml snippet:

<AddedExtras>
    <AddedExtra Code="1234|ABCD" Quantity="1" Supplier="BDA"/>
    <AddedExtra Code="5678|EFGH" Quantity="1" Supplier="BDA"/>
    <AddedExtra Code="9111|ZXYW" Quantity="1" Supplier="BDA"/>
</AddedExtras>

The following XPath expression:

//*["AddedExtra"]/@Code

when run through a checker evaluates to:

Attribute='Code=1234|ABCD'
Attribute='Code=5678|EFGH'
Attribute='Code=9111|ZXYW'

Why then, does the following code only return the first line?

private String allCodes = "//*["AddedExtra"]/@Code";

get my XML from system and parse it into a Doc:

public Document parseResponse(String response){
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder;
    Document doc = null;
    //Create a document reader and an XPath object

    try {
        builder = factory.newDocumentBuilder();
        doc = builder.parse(new InputSource((new StringReader(response))));

    } catch (ParserConfigurationException | org.xml.sax.SAXException | IOException e) {
        e.printStackTrace();
    }
    return doc;
}

Get the new doc:

 public Document getParsedResponse(String response) {
    return parseResponse(response);
}

Return the Xpath value from the doc:

public String getAllCodeOptions(String response){
    Document doc = getParsedResponse(response);
    return getNodeValueFromNodeList(doc, allCodes);
}

new method to read the XML nodes:

public String getNodeValueFromNodeList(Document doc, String expression){
    NodeList nodeList = null;
    String nodes = null;
    try {
        nodeList = (NodeList) xpath.compile(expression).evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    for(int i=0; i < nodeList.getLength(); i++){
        Node node =  nodeList.item(i);
        nodes = node.getNodeValue();
    }
    return nodes;
}

returns:

 Attribute='Code=1234|ABCD'
zx485
  • 28,498
  • 28
  • 50
  • 59
Steerpike
  • 1,712
  • 6
  • 38
  • 71
  • See http://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java – pringi Dec 02 '16 at 13:16
  • Note that your predicate is wrong. The effective boolean value of a non-empty string is true, so `*["AddedExtra"]` matches the same elements as `*`. I'm not going to attempt to answer your question because the question seems to be constantly changing. – Michael Kay Dec 02 '16 at 17:23

2 Answers2

1

You would require to use right evaluate method which takes return type as an argument. Something like below,

NodeSet result = (NodeSet)e.evaluate(e, doc, XPathConstants.NODESET); 
for(int index = 0; index < result.getLength(); index ++) 
{      
  Node node = result.item(index);
  String name = node.getNodeValue();
}
zx485
  • 28,498
  • 28
  • 50
  • 59
Manan Sheth
  • 150
  • 14
0

The problem is that you are asking for only one value.

Try this:

NodeList nodeList = (NodeList)e.evaluate(doc, XPathConstants.NODESET);

for multiple values.

See http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/ for a tutorial.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
pringi
  • 3,987
  • 5
  • 35
  • 45