Skip to content

Conversation

duoduobingbing
Copy link
Contributor

@duoduobingbing duoduobingbing commented Apr 12, 2025

By default there is no easy way with Jackson XML to include standalone='yes' inside the XML declaration. It requires swapping out the xmlWriter which is really cumbersome because the implementations of BaseStreamWriter from Woodstox are final like RepairingNsStreamWriter. Hence to change this the entirety of e.g. RepairingNsStreamWriter needs to be copied to the project only to override BaseStreamWriter#writeStartDocument to call doWriteStartDocument(version, encoding, "yes"); instead of the default doWriteStartDocument(version, encoding, null);.

This PR adds ToXmlGenerator.Feature.WRITE_STANDALONE_YES_TO_XML_DECLARATION which is false by default.

When enabled standalone='yes' will be added to the XML declaration if including it is enabled as well:

XmlMapper mapper = new XmlMapper();
mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
mapper.configure(ToXmlGenerator.Feature.WRITE_STANDALONE_YES_TO_XML_DECLARATION, true);
String xml = mapper.writeValueAsString(Map.of("abc","def"));

will cause the first line to have

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>

There is definetely a need for this as demonstrated by these posts:

@pjfanning
Copy link
Member

Please describe why you even need standalone='yes'. That flag is only supposed to used with DTDs - and hardly anyone uses DTDs any more.

@duoduobingbing
Copy link
Contributor Author

Please describe why you even need standalone='yes'. That flag is only supposed to used with DTDs - and hardly anyone uses DTDs any more.

First of all thank you so much for reviewing this so quickly.

We work with a very nasty consumer on the other end which refuses to read the XML if standalone=yes is missing.

@duoduobingbing duoduobingbing force-pushed the feature/standalone-in-xml-declaration branch 2 times, most recently from ec29218 to 633140f Compare April 12, 2025 13:23
@duoduobingbing duoduobingbing force-pushed the feature/standalone-in-xml-declaration branch 2 times, most recently from e95bf81 to a619e5c Compare April 12, 2025 13:25
@cowtowncoder cowtowncoder added adding-declarations Issues related to adding non-content declarations to XML output 2.19 cla-received PR already covered by CLA (optional label) labels Apr 12, 2025
@cowtowncoder cowtowncoder changed the title add feature to include standalone='yes' in xml declaration Add feature to include standalone='yes' in xml declaration Apr 16, 2025
@cowtowncoder cowtowncoder merged commit 361d2ff into FasterXML:2.19 Apr 16, 2025
4 checks passed
@duoduobingbing duoduobingbing deleted the feature/standalone-in-xml-declaration branch April 27, 2025 12:39
@duoduobingbing duoduobingbing restored the feature/standalone-in-xml-declaration branch April 27, 2025 12:39
@duoduobingbing duoduobingbing deleted the feature/standalone-in-xml-declaration branch April 27, 2025 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.19 adding-declarations Issues related to adding non-content declarations to XML output cla-received PR already covered by CLA (optional label)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants