HandyTools Hub

XML Formatter

Beautify, minify, validate, and convert XML

An XML formatter cleans up dense markup without a round trip to a server. Paste XML and the page can add consistent indentation, collapse it to a minimal payload, check that it is well-formed with exact line and column errors, or convert it to JSON and back. It is routinely useful for SOAP requests and responses, Maven and Android configuration files, sitemaps, and RSS or Atom feeds.

How to Use

  1. Paste or type XML into the input area.
  2. Choose an action: Format, Minify, Validate, XML to JSON, or JSON to XML.
  3. For Format, pick an indentation of 2 or 4 spaces from the dropdown. The other actions ignore it.
  4. Read the output area and copy it with the copy button. Format, minify, and both conversions print errors in red instead of a result when the input is rejected.

Worked Examples

Formatting nested XML. Given <note id="1"><to>Ada</to><to>Bob</to></note> with a 2-space indent, the output is:

<note id="1">
  <to>Ada</to>
  <to>Bob</to>
</note>

Elements whose only content is text stay on one line, which keeps simple leaf nodes compact instead of exploding them into three lines each.

Converting the same document to JSON. The output is:

{"note":{"@id":"1","to":["Ada","Bob"]}}

The element names became keys, the attribute became @id, and the repeated <to> element became an array.

Minifying. Any of the formatted versions collapses back to <note id="1"><to>Ada</to><to>Bob</to></note>, with the whitespace between tags removed but the text content preserved verbatim.

The XML-to-JSON Mapping Rules

XML constructJSON result
Root elementSingle top-level key named after the element
Child elementsNested keys
Repeated siblingsArray under one key
AttributesKeys prefixed with @
Text contentPlain string, or a #text key when attributes or children also exist
Self-closing element with attributesObject of @ keys
Self-closing element without attributesEmpty string
Comments and declarationsDropped

The #text key only appears when an element also carries attributes or child elements; a leaf element containing only text becomes a bare string.

The JSON-to-XML Round Trip

The reverse action accepts a parsed JSON object. A single key becomes the root, so {"book":{"title":"Dune"}} yields <book><title>Dune</title></book>. Multiple top-level keys are wrapped in <root>. An @name key becomes an attribute on its parent element, a #text key becomes the parent’s inline text, and an array repeats the parent key once per item. Text and attribute values are escaped for &, <, >, ", and ', so the produced markup is safe to parse again. A round trip through JSON is useful for feeding XML data to a JavaScript app, but keep in mind the loss of mixed-content ordering and comments noted above.

What the Validator Does and Doesn’t Check

The validator reports: empty input, no tags at all, mismatched open and close tags, closing tags with no matching open, empty tag names, unclosed tags at end of input, and unterminated comments, CDATA sections, or processing instructions. Errors carry a line and column. It does not check schema conformance, DTD rules, namespace correctness, duplicate attributes, or entity resolution. It is a fast lint for the malformed markup that makes servers reject a request, not a standards-complete parser.

Common Mistakes

  • Formatting first when a server rejects the payload. Run Validate instead; it names the exact line and column of the offending tag, which is faster than eyeballing indentation.
  • Assuming minify compresses the file. It removes inter-tag whitespace only, not comments or repeated structure. Gzip on the wire usually saves far more than minification.
  • Mixing up the two conversion directions and pasting JSON into the XML-to-JSON action. The actions are separate; choose the one matching your input.
  • Trusting the JSON shape for single versus repeated elements. A key can flip between a string and an array depending on how many siblings appeared, so defensive code should handle both.

Practical Tips and Larger Inputs

Preserve declarations. The XML declaration and processing instructions keep their own lines and are not indented, so <?xml version="1.0" encoding="UTF-8"?> stays at the top where parsers expect it. Comments and CDATA blocks are likewise preserved verbatim in both format and minify.

Namespace prefixes are part of the tag name. A tag such as <soap:Envelope> is matched by its full string, so opening and closing tags balance as long as the prefix is spelled identically. The tool does not resolve namespace URIs or check that a prefix is declared, which is a schema-level concern.

Large documents. Formatting is a single in-browser pass with no size cap, but files of several megabytes may briefly freeze the tab because the indentation step builds an array of lines. Paste in chunks if the interface stalls; minify is lighter because it only concatenates token values.

Tabs versus spaces. The indent dropdown offers spaces only. If your project uses tabs, format here for readability and let your editor’s formatter rewrite the indentation before you commit. Do not paste formatted output back into an aligned file without reviewing the diff, or you will add whitespace noise to version control.

Frequently Asked Questions

Does the validator check my XSD or DTD schema?

No. It checks well-formedness only — that tags balance, nesting is correct, and comments, CDATA, and processing instructions are closed. A document can pass validation here and still violate its schema, because schema rules such as required attributes or permitted elements are not examined.

How are repeated sibling elements represented in the JSON output?

The first occurrence becomes a single value, and the second occurrence converts that key into an array. So two <to> children produce "to": ["Ada", "Bob"], while a single <to> stays a plain string. Consumers must be ready for either shape.

Why did my id="1" attribute turn into @id in the JSON?

Attributes are prefixed with @ to keep them separate from child elements, which share the same JSON object. The mapping is @id for the attribute and a nested object for children. Reversing the conversion understands the same convention and writes the attribute back.

Does the XML-to-JSON conversion preserve mixed content?

Not faithfully. Text and child elements are stored under separate keys, with the text placed at #text, so the interleaving of runs like bold text mixed with inline tags is lost. If your document relies on mixed content, keep it as XML.

Can I convert JSON back into XML here?

Yes, the JSON to XML action is built in. A JSON object with a single top-level key uses that key as the root element; an object with several keys is wrapped in a <root> element. Keys beginning with @ become attributes and a #text key becomes inline content.

What indentation widths can I choose when formatting?

Two or four spaces, selected from the indent dropdown, which appears only while the Format action is active. The minify action removes all inter-element whitespace regardless of that setting.

Reviewed by the HandyTools Hub team · Last updated