Parsing and processing
Serialization
Turning a document tree back into text — choosing quoting, escaping, empty-element spelling, indentation and encoding.
Parsing loses the choices you made. A tree records that an attribute has a value, not that you wrote it in single quotes; that an element is empty, not that you self-closed it. Serializing therefore cannot reproduce the original bytes, and no correct serializer tries.
This is why round-tripping is not byte-preserving and why comparing XML with a text diff produces noise. Compare trees, or compare canonical forms.
- Defined in
- XML 1.0 §2
- Category
- Parsing and processing
- Also written
- writing XML, output
Shown, not asserted
All 2 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.
The serializer re-adds a declaration and picks its own spelling for an empty element.
document.xml <x></x>What the engine returns
format <?xml version="1.0"?> <x/>It preserves a CDATA section, where a canonicalizer would not.
document.xml <x><![CDATA[a < b]]></x>What the engine returns
format <?xml version="1.0"?> <x><![CDATA[a < b]]></x>
What catches people out
- A round trip is not byte-preserving. If a downstream system compares bytes, canonicalize instead.
- Indentation added on output changes the content of any element with mixed content.
- The output encoding is a serializer setting. Writing UTF-8 bytes under a UTF-16 declaration produces a document nothing can read.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- ToolsXML formatterlibxml2's own serializer, which is why it round-trips CDATA and comments correctly.
- ToolsXML diffCompares structure, so serialization choices do not show up as differences.
- ToolsXML canonicalizer (C14N)The other way to make two equivalent documents comparable.
Related terms
- Canonicalization (C14N)Rewriting a document into a single normal form — sorted attributes, expanded empty elements, resolved references, no declaration — so that equivalent documents become byte-identical.
- Empty elementAn element with no content, written either as a self-closing tag or as a start tag immediately followed by its end tag.
- CDATA sectionA <![CDATA[ … ]]> block whose contents are treated as literal characters, so < and & inside it start nothing.
- Encoding declarationThe encoding pseudo-attribute in the XML declaration, telling the parser which character encoding the file's bytes are in.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.