DevFormatter

XML Formatter

 

How it works

XML (Extensible Markup Language) is a text-based format for storing and transporting structured data. Defined by the W3C, XML uses nested tags to represent hierarchical data — the same idea as HTML, but with custom tag names you define yourself.

This tool uses fast-xml-parser — a fast, DOM-free JavaScript library — to parse and reformat XML entirely in your browser. No data is ever sent to a server.

  • Beautify — parses your XML and re-serializes it with consistent indentation. Also rewrites the input box so you always see clean XML.
  • Minify — strips all whitespace between tags for the most compact representation. Output only — input is unchanged.
  • Validate — checks for well-formedness errors and reports the exact line and column of the first error in the status bar.
  • Convert → JSON — converts XML to a JSON object. Attributes appear as keys prefixed with @_. Output only — input is unchanged.

XML Quick Reference

Construct Syntax Example
Element <tag>content</tag> <name>Alice</name>
Self-closing <tag /> <br />
Attribute name="value" <user id="1">
Comment <!-- text --> <!-- metadata -->
Declaration <?xml version="1.0"?> <?xml version="1.0" encoding="UTF-8"?>
CDATA <![CDATA[...]]> <![CDATA[<b>bold</b>]]>
Entity &amp; &lt; &gt; &lt;script&gt;

XML vs JSON at a Glance

Feature XML JSON
Syntax Tags + attributes Objects + arrays
Comments Yes <!-- --> No
Attributes Yes No (use keys)
Namespaces Yes No
Schema XSD, DTD JSON Schema
Human-readable Verbose Concise
Primarily used for Config, SOAP, RSS, office formats REST APIs, web data

Tips & Tricks

✔

Validate before transforming

Beautify runs validation first — a green status bar means your XML is well-formed and safe to process downstream.

⇄

Use Convert → JSON for REST APIs

Many REST APIs accept JSON but receive XML from legacy systems. Convert in one click and paste directly into your API request body.

🔗

Load live XML feeds

Use the URL button to load RSS, Atom, or SOAP responses directly from a URL. The tool retries with a CORS proxy if the direct request is blocked.

⚡

Minify for storage or transport

Minified XML can be 30–50% smaller than its formatted equivalent for documents with many short values. Use Minify before storing in a database or sending over a network.

≈

Self-closing tags are equivalent

<tag/> and <tag></tag> are identical in well-formed XML. The formatter preserves self-closing tags as-is.

⚠

Check entity encoding

Characters < > & ' " must be escaped as &lt; &gt; &amp; &apos; &quot; in element content and attribute values. Unescaped & is the most common XML parse error.

Code Examples

Node.js — fast-xml-parser

const { XMLParser, XMLBuilder } = require('fast-xml-parser');

const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@_' });
const builder = new XMLBuilder({ ignoreAttributes: false, attributeNamePrefix: '@_', format: true, indentBy: '  ' });

const xml = `Alice`;
const parsed = parser.parse(xml);
const formatted = builder.build(parsed);
console.log(formatted);

Python — xml.dom.minidom & ElementTree

import xml.dom.minidom
import xml.etree.ElementTree as ET

# Pretty-print XML
raw = 'Alice'
dom = xml.dom.minidom.parseString(raw)
print(dom.toprettyxml(indent='  '))

# Parse XML into a tree
tree = ET.parse('config.xml')
root = tree.getroot()
for child in root:
    print(child.tag, child.attrib)

CLI — xmllint

# Format XML with xmllint (install: brew install libxml2)
xmllint --format file.xml

# Validate XML against a schema
xmllint --schema schema.xsd file.xml

# Format and save
xmllint --format file.xml -o formatted.xml

# Convert XML to JSON with python
python3 -c "import sys,json,xml.etree.ElementTree as ET; \
  t=ET.parse(sys.argv[1]); print(json.dumps({t.getroot().tag:{}}))" file.xml

Frequently Asked Questions

What does XML stand for?
XML stands for Extensible Markup Language. It is a text-based format for storing and transporting structured data, defined by the W3C. Unlike HTML, XML has no predefined tags — you define your own tag names to describe your data. XML is used in configuration files, APIs (SOAP, RSS, Atom), office document formats (DOCX, XLSX), and data interchange between systems.
What is an XML formatter?
An XML formatter (also called an XML beautifier or XML pretty printer) takes compact or minified XML and adds indentation and line breaks so the structure is human-readable. It parses the XML into a tree, then serializes it back with consistent indentation. A formatter also validates the XML — if the input is not well-formed, it reports the exact line and column of the error.
Is my XML data sent to a server?
No. All formatting, validation, and conversion runs entirely in your browser using JavaScript. Your data never leaves your machine and no account or sign-up is required.
What is the difference between well-formed and valid XML?
Well-formed XML follows the basic XML syntax rules: every opening tag has a matching closing tag, attributes are quoted, tags are properly nested, and there is exactly one root element. Valid XML additionally conforms to a schema (DTD or XSD) that defines which elements and attributes are allowed. This formatter checks for well-formedness — schema validation requires a separate XSD validator.
What are the most common XML errors?
The most common XML errors are: unclosed tags (every opening tag needs a closing tag or self-closing form), mismatched tags, unquoted attribute values, special characters not escaped as entities (&amp; &lt; &gt; &apos; &quot;), and multiple root elements (XML requires exactly one root). The formatter reports the exact line and column of the first error.
How do I convert XML to JSON?
Use the Convert → JSON button. XML attributes are included in the JSON output prefixed with @_ (e.g. <user id="1"> becomes {"user": {"@_id": "1"}}). Note that XML's ordered elements and repeated sibling tags do not map cleanly to JSON objects — review the output carefully for elements with multiple children of the same tag name.
What is the difference between XML and HTML?
HTML is a specific application of markup designed for web pages with fixed tags (div, p, img, etc.) and a forgiving parser that handles unclosed tags and other errors. XML is a general-purpose format where you define your own tags, and the parser is strict — any syntax error stops processing. XHTML is HTML reformulated as strict XML. Modern web development uses HTML5 (not XML-based), while XML is used for data interchange, configuration, and document formats.
What are XML namespaces?
XML namespaces prevent tag name collisions when combining XML from different sources. A namespace is declared with the xmlns attribute: <root xmlns:app="https://example.com/app">. Tags in that namespace use the prefix: <app:user>. Namespaces are common in SOAP web services, SVG, and Office Open XML formats. The formatter preserves namespace declarations in the output.
How do I minify XML?
Click the Minify button to remove all whitespace between tags and produce the most compact valid XML. In Node.js, use fast-xml-parser's XMLBuilder with format: false. In Python, use xml.etree.ElementTree to parse and re-serialize without pretty-printing. Minified XML reduces file size for network transfer and storage.
What is CDATA in XML?
CDATA (Character Data) sections let you include text containing XML special characters without escaping them. A CDATA section looks like <![CDATA[ raw content here ]]>. Everything inside is treated as literal text, not markup. CDATA is useful for embedding HTML, code snippets, or SQL inside XML documents. The formatter preserves CDATA sections in the output.
What is the XML declaration?
The XML declaration is an optional first line: <?xml version="1.0" encoding="UTF-8"?>. It specifies the XML version (always 1.0 in practice) and the character encoding. If omitted, parsers assume UTF-8. The declaration must appear before the root element and before any other content. It is recommended for documents that may be processed by systems with different default encodings.
What is XPath and how is it used?
XPath is a query language for selecting nodes in an XML document. It uses path expressions like /root/users/user[@id='1']/name to navigate the tree. XPath is used in XSLT transformations, Selenium web testing, and XML processing in Python (xml.etree.ElementTree supports XPath subsets). XSLT uses XPath to define which XML nodes to transform and how to render them.

Related Tools

Other free developer tools available on DevFormatter.