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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.