1

The XML file is as :

Xml File

Code I have written:

List queryXmlUsingXpathAndReturnList(String xml, String xpathExpression) {

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance()

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder()

Document doc = dBuilder.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)))

doc.getDocumentElement().normalize()

XPath xPath = XPathFactory.newInstance().newXPath()

NodeList nodeList = (NodeList) xPath.compile(xpathExpression).evaluate(doc, XPathConstants.NODESET)

List returnElements = new ArrayList<>()

nodeList.each { n ->

returnElements.add(n.getTextContent())

}

When I am passing the xpath as:

/Envelope/Body/CommandResponseData/OperationResult/Operation/ParameterList/ListParameter/StringElement

It returns all the values. But I want to return only the ListParameter values whose name="PackageTypeList".

For that I am using the xpath as:

/Envelope/Body/CommandResponseData/OperationResult/Operation/ParameterList/ListParameter[@name='PackageTypeList']/StringElement

But it returns list as null.

1
  • Pictures are useless. Please edit your post and add your XML as a text. Commented Nov 25, 2020 at 18:54

1 Answer 1

0

I guess you miss "CommandResult" between "CommandResponseData" and "OperationResult" in your XPath-Expression.

Sign up to request clarification or add additional context in comments.

1 Comment

It seems wrongly I pasted it. My xpath is: /Envelope/Body/CommandResponseData/CommandResult/OperationResult/Operation/ParameterList/ListParameter[@name='PackageList']/StringElement Still getting null response. If I will remove [@name='PackageList'] , I am getting all the values, but as I mentioned I need specific data whose name=PackageList.

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.