0

<?xml version="1.0" encoding="utf-8"?> <double xmlns="http://www.somewebsite.com/">2.0</double>

I'm having a bit of trouble parsing this using XPath 1.0.

this is what i do:

XPath xpath = XPath.newInstance("/double"); Element returnElement = (Element) xpath.selectSingleNode(doc);

the return element is null but it should be 2.0.

NOTE: It should be using XPath 1.0

2

1 Answer 1

0

The double element is in the http://www.somewebsite.com/ namespace. Map the namespace to a prefix (e.g. foo) and resolve with a qualified expression (e.g. /foo:double).

Using the standard API:

String xml = "<double xmlns='http://www.somewebsite.com/'>2.0</double>";
Reader reader = new StringReader(xml);
XPath xpath = XPathFactory.newInstance()
                          .newXPath();
NamespaceContext context = new NamespaceContextMap("foo", "http://www.somewebsite.com/");
xpath.setNamespaceContext(context);
String value = xpath.evaluate("/foo:double", new InputSource(reader));
System.out.println(value);

You can find a sample implementation of NamespaceContext on my blog.

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.