0

The input to the function this code is in, is a Node configNode. I need to extract the value of a child node inTemplate. The following is the code. Only null is printed.

XPath xpath = XPathFactory.newInstance().newXPath();
Node inTemplateNode = (Node) xpath.compile("@inTemplate").evaluate(configNode, XPathConstants.NODE);
String inTemplate = (inTemplateNode != null) ? inTemplateNode.getTextContent() : null;
        System.out.println("inTemplate Value =" + inTemplate);

Can anyone help me as to why this code is not working.

1
  • It would be helpful if you also post the XML file. Commented Feb 17, 2016 at 16:18

1 Answer 1

1

The XPath expression @inTemplate selects the attribute named inTemplate of the context node (e.g. <config inTemplate="foo"/>). If you really need an attribute value then doing ((Element)configNode).getAttribute("inTemplate") should work in the DOM without the need to use any XPath.

If you want to select a child element (e.g. <config><inTemplate>foo</inTemplate></config>) named inTemplate then use the path inTemplate and not @inTemplate.

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.