Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,8 @@ Bas Passon (@bpasson)
* Reported, contributed fix for #646: Deserializing fails when using builder classes
with `Iterable` Collection setters
(2.17.1)

多多冰冰 (@duoduobingbing)

* Contributed #745: Add feature to include `standalone='yes'` in xml declaration
(2.19.0)
5 changes: 4 additions & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ Project: jackson-dataformat-xml
=== Releases ===
------------------------------------------------------------------------

2.19.0-rc (07-Apr-2025)
#745: Add feature to include `standalone='yes'` in xml declaration
(contributed by @duoduobingbing)

2.19.0-rc2 (07-Apr-2025)

#700: Unify testing structure/tools [JSTEP-10]
(fix contributed by Joo Hyuk K)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public enum Feature implements FormatFeature
*/
WRITE_XML_DECLARATION(false),

/**
* Feature that controls whether XML declaration should include the standalone attribute
* when generator is initialized (true) or not (false). Only honored when
* {@link Feature#WRITE_XML_DECLARATION WRITE_XML_DECLARATION} is enabled
*
* @since 2.19
*/
WRITE_STANDALONE_YES_TO_XML_DECLARATION(false),

/**
* Feature that controls whether output should be done as XML 1.1; if so,
* certain aspects may differ from default (1.0) processing: for example,
Expand Down Expand Up @@ -297,15 +306,24 @@ public void initGenerator() throws IOException
_initialized = true;
try {
boolean xmlDeclWritten;
if (Feature.WRITE_XML_1_1.enabledIn(_formatFeatures)) {
_xmlWriter.writeStartDocument("UTF-8", "1.1");
xmlDeclWritten = true;
} else if (Feature.WRITE_XML_DECLARATION.enabledIn(_formatFeatures)) {
_xmlWriter.writeStartDocument("UTF-8", "1.0");

if (Feature.WRITE_XML_1_1.enabledIn(_formatFeatures) ||
Feature.WRITE_XML_DECLARATION.enabledIn(_formatFeatures)) {

String xmlVersion = Feature.WRITE_XML_1_1.enabledIn(_formatFeatures) ? "1.1" : "1.0";
String encoding = "UTF-8";

if (Feature.WRITE_STANDALONE_YES_TO_XML_DECLARATION.enabledIn(_formatFeatures)) {
_xmlWriter.writeStartDocument(xmlVersion, encoding, true);
} else {
_xmlWriter.writeStartDocument(encoding, xmlVersion);
}

xmlDeclWritten = true;
} else {
xmlDeclWritten = false;
}

// as per [dataformat-xml#172], try adding indentation
if (xmlDeclWritten && (_xmlPrettyPrinter != null)) {
// ... but only if it is likely to succeed:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,26 @@ public void testXml11Declaration() throws Exception
String xml = mapper.writeValueAsString(new StringBean("abcd"));
assertEquals(xml, "<?xml version='1.1' encoding='UTF-8'?><StringBean><text>abcd</text></StringBean>");
}

@Test
public void testXml11DeclarationWithStandalone() throws Exception
{
XmlMapper mapper = new XmlMapper();
mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
mapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
mapper.configure(ToXmlGenerator.Feature.WRITE_STANDALONE_YES_TO_XML_DECLARATION, true);
String xml = mapper.writeValueAsString(new StringBean("abcd"));
assertEquals("<?xml version='1.1' encoding='UTF-8' standalone='yes'?><StringBean><text>abcd</text></StringBean>", xml);
}

@Test
public void testXml10DeclarationWithStandalone() throws Exception
{
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(new StringBean("abcd"));
assertEquals("<?xml version='1.0' encoding='UTF-8' standalone='yes'?><StringBean><text>abcd</text></StringBean>", xml);
}

}