JDK-8217937 : Release Note: Change DOM Parser to Not Resolve EntityReference and Add Text Node with DocumentBuilderFactory.setExpandEntityReferences(false)
  • Type: Sub-task
  • Component: xml
  • Sub-Component: javax.xml.parsers
  • Affected Version: 13
  • Priority: P4
  • Status: Closed
  • Resolution: Delivered
  • Submitted: 2019-01-28
  • Updated: 2019-08-09
  • Resolved: 2019-02-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 13
The implementation of the `ExpandEntityReferences`  feature was changed to comply with the specification of the `DocumentBuilderFactory.setExpandEntityReferences` method. Specifically, now when the method is set to `false` and encounters an entity reference, a DOM parser created by the `DocumentBuilderFactory` adds the `EntityReference` node to the DOM tree without the expanded Text node. Before the change, the implementation incorrectly added both nodes.

With the change, the DOM parser no longer reads and resolves entity references when the feature `ExpandEntityReferences`  is set to false. For applications that intend to avoid resolving entity references, it will work as expected.

This change also affects the DOM Load and Save parser. The `entities` parameter is aligned with the `ExpandEntityReferences` feature, so that setting the `entities` parameter to `true` is equivalent to setting `ExpandEntityReferences` to `false`. In the following code snippet, the `document` will contain `EntityReference` nodes but not expanded `Text` nodes:
        LSParser domParser = domImplementationLS.createLSParser(MODE_SYNCHRONOUS, null);
        domParser.getDomConfig().setParameter("entities", true);
        LSInput src = domImplementationLS.createLSInput();
        Document document = domParser.parse(src);
Because the references are not resolved, the resulting string will contain entity references without the text when the `document` is serialized:
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
        lsSerializer.getDomConfig().setParameter("format-pretty-print", true);
        String result = lsSerializer.writeToString(document);

Reviewed by Lance and Daniel.