Class XmlCodec
javax.xml.parsers/DOM.
Reading: always parses namespace-aware; a document's single root element becomes the sole top-level edge; child elements become nested edges, preserving repeated-element order (this is the one codec where cross-label interleaving both reads and writes faithfully — every other codec's writer groups same-label edges). Attribute and namespace-prefix information is discarded on read, silently per omnist-spec §9.4 D-3 (no diagnostic is emitted for this — it's a spec-mandated lossy conversion, not an oversight).
Writing: scalar leaves are stringified per XML's own type rules
(XML_INT_RE/XML_NUM_RE); a leaf label that isn't a legal XML
name is sanitized, falling back to an underscore-prefixed form.
This class is stateless; all methods are static.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum accepted input length in characters, guarding against oversized XML input. -
Method Summary
Modifier and TypeMethodDescriptionstatic WriteReportComputes what adjustmentswrite(Document)would make tonodewithout actually producing XML text.static DocumentParses XML text into aDocumentwithout schema guidance.static DocumentParses XML text into aDocument, optionally with schema guidance.static StringSerializes aDocumentto XML text, non-strict (dropping/sanitizing unrepresentable content rather than throwing).static Stringwrite(Document node, boolean strict, WriteReport report) Serializes aDocumentto XML text.
-
Field Details
-
MAX_INPUT_LENGTH
public static final int MAX_INPUT_LENGTHMaximum accepted input length in characters, guarding against oversized XML input.- See Also:
-
-
Method Details
-
read
Parses XML text into aDocumentwithout schema guidance. Equivalent toread(text, null).- Parameters:
text- the XML text; must not benull- Returns:
- the parsed document
- Throws:
RuntimeException- if the XML is not well-formed or exceedsMAX_INPUT_LENGTH
-
read
Parses XML text into aDocument, optionally with schema guidance.Unlike the other three format codecs (whose
read(text, schema)overload ignoresschemaentirely, or in JsonCodec's case accepts it without acting on it — schema-driven coercion there happens in a laterMaterializerstage instead), XML text is inherently ambiguous about scalar kind: every attribute and element value is just a string until something says otherwise. Whenschemais non-null, its root record's field types are used to pre-resolve strings that look like booleans/integers/numbers into the matching Java type before the document is built, so a later validate/materialize call sees a typed value rather than a string it would otherwise have to coerce blind.- Parameters:
text- the XML text; must not benullschema- if non-null, guides scalar-kind resolution for ambiguous string content as described above; ifnull, every scalar is read as a plain string- Returns:
- the parsed document
- Throws:
RuntimeException- if the XML is not well-formed or exceedsMAX_INPUT_LENGTH
-
write
Serializes aDocumentto XML text, non-strict (dropping/sanitizing unrepresentable content rather than throwing). Equivalent towrite(node, false, null).- Parameters:
node- the document to serialize; must have exactly one top-level edge (XML requires a single root element)- Returns:
- the XML text
- Throws:
WriteException- ifnodedoes not have exactly one top-level edge
-
write
Serializes aDocumentto XML text.- Parameters:
node- the document to serialize; must have exactly one top-level edge (XML requires a single root element)strict- iftrue, throws when the document contains any adjustment (e.g. a sanitized element name); iffalse, applies the adjustment and continuesreport- if non-null, every adjustment made during writing is appended here- Returns:
- the XML text
- Throws:
WriteException- ifnodedoes not have exactly one top-level edge, or ifstrictistrueand an adjustment was required
-
check
Computes what adjustmentswrite(Document)would make tonodewithout actually producing XML text.- Parameters:
node- the document to check- Returns:
- the adjustments (e.g. sanitized names, dropped nulls) that a write of
nodewould require
-