I was parsing an XML document, in Java, using the following code:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Element el = db.parse(file).getDocumentElement();
However, when parsing an XML document that uses namespaces, I get the error:
org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create
or change an object in a way which is incorrect with regard to namespaces.
at org.apache.xerces.dom.ElementNSImpl.setName(Unknown Source)
I solved this error by adding the line:
dbf.setNamespaceAware(true);
Now everything works.
It would seem to me that setting the parser to be namespace-aware would be a purely positive thing. As far as I understand, it can still handle "vanilla" non-namespace documents just fine, without any disadvantages.
But yet, if that were the case, then why did they make it an option, and why false
by default?
So my question is: What is a good default for me to use in my software? Should I always set it to true
(because the designers of these APIs messed up by offering it as an option at all) or should I leave it as false
in some cases because each value has some advantages? If so, what are the advantages of false
?
You don't have to create a document with a parse. You could create the document via createDocument, createElement etc. In a non-namespace-aware document, colons are a valid character of the node name, so you could, for instance, have an element <foo:bar:baz>
.
Code written with such elements before XML namespaces were introduced would work fine. So for backward compatibility, when support for namespaces was introduced, the only way to keep that code working without changes, was to make namespace awareness false by default, because <foo:bar:baz>
is not legal in a namespace aware document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With