Namespaces

An important addition to DOM Level 2 not yet discussed is DOM’s support for XML namespaces. You’ll remember from Chapter 3 and Chapter 4, that SAX 2.0 added namespace support, and the same is true for the second iteration of DOM. The key here is two new methods on the Node interface: getPrefix( ) and getNamespaceURI( ) . Additionally, all of the creation methods have namespace-aware versions available. So, instead of calling createElement( ), you call createElementNS( ).

In each of these new namespace-aware methods, the first argument is the namespace URI, and the second is the qualified name of the element, attribute, etc. Note that I said qualified; this means that if you want to use a namespace URI of “http://www.oreilly.com” and a prefix of “ora” on an element called “copyright”, you would call createElementNS("http://www.oreilly.com", "ora:copyright"). This is very ...

Get Java and XML, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.