Serializing

Serialization means to write XML back to a file (or string or array). At this point, it likely won't suprise you that cxml's serialization code is based on SAX, given that SAX is already a bit like serialization (only without the actual formatting and escaping).

The serialization code is implemented in a SAX handler called a sink. You just send SAX events to the sink, and get a serialized document back:

CL-USER> (stp:serialize *doc* (cxml:make-string-sink))
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<elt>foo</elt>"

(Other sinks besides the stream sink include octet-stream and octet-vector sinks.)

By now, the ability to round-trip from string to string will seem like an old hat:

CL-USER> (cxml:parse "<test>&#32;</test>" (cxml:make-string-sink))
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<test> </test>"

But note how we've just implemented the equivalent of the "xmllint" program as a one-liner!

The round-trip preserves the content model of the document, but formatting aspects will be lost. For example, the character reference doesn't actually need escaping, and the output encoding will be UTF-8, even if you had parsed a file in a different encoding.