I am trying to get node value of XML by XPath expression like below code.
It returns empty although it returns the correct node value when evaluating it on xpath-tester
private String getNodeValue(){
String nodeVal=null;
try {
XPath xPath = XPathFactory.newInstance().newXPath();
String testData="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+ "<provider xmlns=\"http://www.test.com/test-message\">\r\n" +
"<header>\r\n" +
"<TmStp>2013-12-25T08:52:595</TmStp>\r\n" +
"<GUID>testguid64886</GUID>\r\n" +
"</header>\r\n" +
"<service>\r\n" +
"<name>name</name>\r\n" +
"<type>type</type>\r\n" +
"</service>\r\n" +
"</provider>";
InputSource inputSource = new InputSource(new StringReader(testData));
nodeVal = (String) xPath.compile("/provider/header").evaluate(inputSource);
System.out.println("Node Value "+nodeVal); //output is empty >>>> Node Value []
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return nodeVal;
}
The raw XML part of the code above is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<provider xmlns="http://www.test.com/test-message">
<header>
<TmStp>2013-12-25T08:52:595</TmStp>
<GUID>testguid64886</GUID>
</header>
<service>
<name>name</name>
<type>type</type>
</service>
</provider>
And the raw XPath expression is:
/provider/header