Add newInstance methods that turn on Namespace support by default
When the XML Namespace support was introduced in Java SE 1.4, it made the default setting of NamespaceAware feature false in order to maintain backward compatibility. To enable Namespace support, a method "setNamespaceAware" has to be called as demonstrated below:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // turn on namespace support dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder();
The problem is that users are often time not realizing or forgetting that the default setting for DOM and SAX is not Namespace aware and attempt to create a parser with a method chain such as:
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
and then encounter processing errors such as missing namespaces or not being able to read namespace (e.g. a call to lookupNamespaceURI returns null).
While such issues can be resolved by setting Namespace aware, it is nonetheless a nuisance to the development process.
Namespace support has been in the Java SE since 1.4. Since then, XML Namespace has become a norm for XML development. Having a parser that supports Namespace by default is long overdue and would be more user-friendly.
In the feature request, we would like to propose adding newInstance methods that shall turn on Namespace by default. The benefit is not only eliminating the trouble users may encounter as described in the problem section, but also reminding users that the old/existing corresponding methods would create factory instances that have Namespace support turned off.
The new methods add "NS" for Namespace as a prefix to the existing methods. With these new methods, users would be able to a chained method to create a parser with Namespace support, for DOM:
DocumentBuilder db = DocumentBuilderFactory.newNSInstance().newDocumentBuilder();
and for SAX:
SAXParser sp = SAXParserFactory.newNSInstance().newSAXParser();
- csr of
JDK-8219692 DOM and SAX parsers ignore namespace