« Converting SOAP to XML | Main | The other way round... »

March 09, 2006

Java 5, XPath and independent namespace declarations

Easy use of XPath expression with XML documents seems to be new in Java 5 (see
XML Validation and XPath Evaluation in J2SE 5.0
).

Playing around with javax.xml.xpath.XPath led me to setNamespaceContext that enables you to provide an independent mapping of namespace prefixes to namespace URIs.

Sample XML-File:

<abc:root xmlns:abc="http://www.abc.com">
 <abc:element>Foo</abc:element>
</abc:root>

By typing

nsCtxImpl.setNamespaceURI("xyz","http://www.abc.com");

you can use the XPath-Expression "/xyz:root/xyz:element" instead of abc. As long as the namespace URI stays the same the prefix may be changed to anything by this...

Posted by Dominik Marks at March 9, 2006 05:41 PM

Comments

Post a comment




Remember Me?

(you may use HTML tags for style)