I am trying to read attribute from CustomXmlPart using xpathGetString
String abcd = lc_CustomXmlPart.xpathGetString( "/Contract/Currency[1]/@Code", "" );
System.out.println( abcd );
But it always returns empty string.
Xml looks like this:
<Contract xmlns="http://abc.123.cz" TypeCode="HO" IssueDate="2017-02-11">
<Currency Code="EUR" Name="Default" PrintCode="€"></Currency>
</Contract>
Can someone please tell me what am I doing wrong?
The XML defines a default namespace (xmlns
) and you're trying to access its nodes without specifying that namespace.
You can change your XPath to access nodes of any namespace :
in XPath 1.0 : /*[local-name()='Contract']/*[local-name()='Currency'][1]/@Code
in XPath 2.0 : /*:Contract/*:Currency/@Code
But it would be best to specify it :
String abcd = lc_CustomXmlPart.xpathGetString( "/abc:Contract/abc:Currency[1]/@Code", "xmlns:abc=\"http://abc.123.cz\"" );
I'm not sure about the exact syntax, check your API documentation ; what I'm sure about is that there is a way to specify prefix mappings which you'll be able to use in your XPath.
Note that there might be a way to specify a default namespace for your XPath query which would lead to the same result (selecting the node of the specific namespace) without having to modify your XPath.