How do I use XML namespaces with DOM level 2?
XML Interview Questions and Answers
(Continued from previous question...)
103. How do I use XML namespaces with DOM level 2?
// Check the local name.
// getNodeName() is a DOM level 1 method.
if (elementNode.getNodeName().equals("SalesOrder"))
{
// Add new database record.
}
might become the following namespace-aware code:
// Check the XML namespace name (URI).
// getNamespaceURI() is a DOM level 2 method.
String SALES_NS = "http://www.foo.com/ito/sales";
if (elementNode.getNamespaceURI().equals(SALES_NS))
{
// Check the local name.
// getLocalName() is a DOM level 2 method.
if (elementNode.getLocalName().equals("SalesOrder"))
{
// Add new database record.
}
}
Note that, unlike SAX 2.0, DOM level 2 treats xmlns attributes as normal attributes.
(Continued on next question...)
Other Interview Questions
|