Given the following XML (example):
<?xml version="1.0" encoding="UTF-8"?> <rsb:VersionInfo xmlns:atom="http://www.w3.org/2005/Atom" xmlns:rsb="http://ws.rsb.de/v2"> <rsb:Variant>Windows</rsb:Variant> <rsb:Version>10</rsb:Version> </rsb:VersionInfo>
I need to get the Variant and Version values. My current approach uses XPath as I cannot rely on this structure. All I know is that there is an rsb:Version element in the document.
XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "//Variant"; InputSource inputSource = new InputSource("test.xml"); String result = (String) xpath.evaluate(expression, inputSource, XPathConstants.STRING); System.out.println(result);
This, however, does not display anything. I tried the following XPath expressions:
- // Option
- // Variant / text ()
- // RSB: Option
- // RSB: Variant / text ()
What is the correct XPath expression? Or is there an even easier way to get to this element?
source share