Skip to main content
XMLDir

Search 309 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

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.

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.