2

i need to get the xlink:label value(ASSET_1) from XML.

<MESSAGE xmlns:xlink="http://www.w3.org/1999/xlink">
<ABOUT_VERSIONS>
<ABOUT_VERSION SequenceNumber="1"  xlink:label="ASSET_1" >
<CreatedDatetime>2015-08-24T09:30:47Z</CreatedDatetime>
<DataVersionName>Purchase Example</DataVersionName>
</ABOUT_VERSION>
</ABOUT_VERSIONS>
</MESSAGE>

The Java Code i am trying is like below

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

XPathExpression pathExpression = xPath.compile("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION");   
InputSource inputSource = new InputSource("C:/Sample.xml");  
NodeList Nodes = (NodeList) xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION", inputSource, XPathConstants.NODESET);

System.out.println("SequenceNumber:: "+xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION/@SequenceNumber", inputSource, XPathConstants.NODE));
System.out.println(" "+xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION/@xlink:label", inputSource, XPathConstants.NODE));
OutPut
SequenceNumber:: SequenceNumber="1"

null

What is the mistake i am doing to pull the value of xlink:label ? Please help.

Bill P
  • 3,622
  • 10
  • 20
  • 32
VKP
  • 589
  • 1
  • 8
  • 34

1 Answers1

1

You can use @*[name()='xlink:label'] in place of @xlink:label. Also switching to @*[local-name()='label'] should do the trick.

Kamil
  • 2,712
  • 32
  • 39
  • No , its still giving me null . – VKP Oct 05 '18 at 19:53
  • All calls to ```System.out.println(" "+xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION/@xlink:label", inputSource, XPathConstants.NODE));```, ```System.out.println(" "+xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION/@*[name()='xlink:label']", inputSource, XPathConstants.NODE));``` and ```System.out.println(" "+xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION/@*[local-name()='label']", inputSource, XPathConstants.NODE));``` give you nulls? – Kamil Oct 05 '18 at 19:56
  • Yes , All are returning null . I doubt do we need to register namespace for using the xlink . I am not sure . Just a thought. – VKP Oct 05 '18 at 19:59
  • That might help, but I'm not sure why none of previously mentioned ways do work. I checked it on Oracle Java 1.8.0_181. – Kamil Oct 05 '18 at 20:05
  • My Bad , I just deleted the XML and recreted and tried with your code . Boom . It started working . Thanks. – VKP Oct 05 '18 at 20:42