Are there any disadvantages to setNamespaceAware(true)?

不想你离开。 提交于 2019-12-12 12:49:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/49790117/are-there-any-disadvantages-to-setnamespaceawaretrue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!