Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ namespace Aws
*/
bool enableHttpClientTrace = false;

/**
* Serialize XML request payloads without pretty-printing whitespace (indentation and newlines),
* producing a smaller payload. Defaults to false to preserve existing serialization behavior.
*/
bool compactXmlSerialization = false;

/**
* profileName in config file that will be used by this object to resolve more configurations.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ namespace Aws
* Convert entire document to string. Use this if you for example, want to save the document to a file.
*/
Aws::String ConvertToString() const;
/**
* Convert entire document to string. When compact is true, the output omits pretty-printing
* whitespace (indentation and newlines), producing a smaller payload.
*/
Aws::String ConvertToString(bool compact) const;
/**
* Returns true if the call to CreateFromXml* was successful, otherwise false.
* if this returns false, you can call GetErrorMessage() to see details.
Expand Down
7 changes: 6 additions & 1 deletion src/aws-cpp-sdk-core/source/utils/xml/XmlSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,15 @@ Aws::String XmlDocument::GetErrorMessage() const
}

Aws::String XmlDocument::ConvertToString() const
{
return ConvertToString(false);
}

Aws::String XmlDocument::ConvertToString(bool compact) const
{
if (!m_doc) return "";

Aws::External::tinyxml2::XMLPrinter printer;
Aws::External::tinyxml2::XMLPrinter printer(nullptr, compact);
printer.PushHeader(false, true);
m_doc->Accept(&printer);

Expand Down
25 changes: 25 additions & 0 deletions tests/aws-cpp-sdk-core-tests/utils/xml/XmlSerializerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ TEST_F(XmlSerializerTest, TestXmlSerialize)
ASSERT_TRUE(doc.GetErrorMessage().empty());
}

TEST_F(XmlSerializerTest, TestXmlSerializeCompact)
{
XmlDocument doc = XmlDocument::CreateWithRootNode("ToDo");
XmlNode rootElement = doc.GetRootElement();
XmlNode item1 = rootElement.CreateChildElement("Item");
item1.SetAttributeValue("priority", "1");
item1.SetText("Go to the <bold>Toy store!</bold>");
XmlNode item2 = rootElement.CreateChildElement("Item");
item2.SetAttributeValue("priority", "2");
item2.SetText("Do bills");

Aws::String serializedXml = doc.ConvertToString(true);

Aws::String toCompare = "<?xml version=\"1.0\"?>"
"<ToDo>"
"<Item priority=\"1\">Go to the &lt;bold&gt;Toy store!&lt;/bold&gt;</Item>"
"<Item priority=\"2\">Do bills</Item>"
"</ToDo>";
ASSERT_EQ(toCompare, serializedXml);
ASSERT_TRUE(doc.GetErrorMessage().empty());

// The no-arg overload preserves the pretty-printed default.
ASSERT_EQ(doc.ConvertToString(false), doc.ConvertToString());
}

TEST_F(XmlSerializerTest, TestXmlSerializeNewlines)
{
XmlDocument doc = XmlDocument::CreateWithRootNode("Newlines");
Expand Down
Loading