Data Transformation intermediate 12 min

Inspect and Fix a Messy XML Response

Format XML before deciding whether the response is malformed or merely hard to read.

Scenario

An integration partner returns XML as one dense line, and your parser fails with something like mismatched tag at line 1, column 58 — a location that is useless when the whole document is line 1. Minified XML makes every structural question (is <item> inside <items>? did an attribute lose its quote?) unanswerable by eye. Pretty-print it first; most "XML bugs" become visible the moment the nesting is indented.

Broken input

<order><id>42</id><items><item sku="A1">Widget</item></items></order>

Goal

Use XML Formatter & Validator to format and validate the XML without losing attributes or text nodes. A readable tree turns the parser's line-1-column-58 complaint into a specific element you can point at.

Tool sequence

  1. Open XML Formatter & Validator.
  2. Paste the single-line response exactly as the API returned it, before any manual editing.
  3. Format it and let the validator run — a well-formedness error here means the payload is broken at the source, not in your parser.
  4. Read the indented tree: order wraps id and items, items wraps one item, and the sku="A1" attribute is still attached to the item element.
  5. If the payload must become JSON for downstream code, convert it only after this validation pass, and pretty-print the result with JSON Formatter.

Checkpoint output

<order>
  <id>42</id>
  <items>
    <item sku="A1">Widget</item>
  </items>
</order>

The structure is valid and readable: nesting is intact and the sku attribute survived formatting. Whatever broke the parser, it was not this document's shape.

Common mistakes

  • Converting XML to JSON before confirming the XML is well-formed — the converter's error will be even vaguer than the parser's.
  • Dropping attributes while manually reformatting, then hunting a "data loss" bug you introduced yourself.
  • Fixing the tag the error message names without re-validating the whole tree; one unclosed tag often cascades into several phantom errors.
  • Trusting indentation someone added by hand — pretty whitespace does not mean valid nesting.

Related tools and lessons