Skip to content
Home/Guides/XML Formatter — How to Format, Validate, and Beautify XML Online
Guide·

XML Formatter — How to Format, Validate, and Beautify XML Online

XML remains the backbone of enterprise systems, SOAP APIs, RSS feeds, SVG graphics, Android layouts, Maven configs, and countless data interchange formats. Despite JSON's popularity for web APIs, XML is not going away — too much critical infrastructure depends on it. And raw XML from APIs, log files, and config exports is just as unreadable as raw JSON — deeply nested elements crammed into a single line with no indentation.

This guide covers what XML formatting and validation do, how to handle XML programmatically, and the common errors that break XML parsers.

What Is XML Formatting?

XML formatting adds consistent indentation, line breaks, and alignment to XML documents to make their hierarchical structure visible. Each element starts on its own line. Child elements are indented inside their parents. Attributes are spaced consistently. The formatter also validates well-formedness — checking that every opening tag has a closing tag, attributes are quoted, and the document has a single root element.

You would use XML formatting when debugging SOAP API responses, inspecting Android layout files, reviewing Maven or Gradle configuration, reading RSS feeds, editing SVG graphics, and working with any XML-based config format.

How to Format XML with FlipMyCase

  1. Open the FlipMyCase XML Formatter.
  2. Paste your unformatted XML.
  3. The tool validates, formats, and syntax-highlights it instantly.
  4. Copy the formatted output or switch to Minify mode for compact XML.

The formatter handles XML declarations, namespaces, CDATA sections, processing instructions, and comments. For working with JSON instead, use the JSON Formatter.

Code Examples for XML Formatting

JavaScript (with xml-formatter)

const xmlFormatter = require('xml-formatter');

const ugly = '<?xml version="1.0"?><catalog><book id="1"><title>JavaScript Guide</title><author>Dev Team</author><price>29.99</price></book><book id="2"><title>Python Handbook</title><author>Dev Team</author><price>34.99</price></book></catalog>';

const formatted = xmlFormatter(ugly, {
  indentation: '  ',
  collapseContent: true,
  lineSeparator: '\n',
});

console.log(formatted);
// <?xml version="1.0"?>
// <catalog>
//   <book id="1">
//     <title>JavaScript Guide</title>
//     <author>Dev Team</author>
//     <price>29.99</price>
//   </book>
//   <book id="2">
//     <title>Python Handbook</title>
//     <author>Dev Team</author>
//     <price>34.99</price>
//   </book>
// </catalog>

// Parse XML to object (with xml2js)
const { parseString } = require('xml2js');
parseString(ugly, (err, result) => {
  console.log(result.catalog.book[0].title[0]);
  // JavaScript Guide
});

Python (with xml.dom.minidom)

import xml.dom.minidom

ugly = '<?xml version="1.0"?><catalog><book id="1"><title>JavaScript Guide</title><author>Dev Team</author><price>29.99</price></book><book id="2"><title>Python Handbook</title><author>Dev Team</author><price>34.99</price></book></catalog>'

dom = xml.dom.minidom.parseString(ugly)
formatted = dom.toprettyxml(indent='  ')
# Remove extra XML declaration if already present
lines = formatted.split('\n')
if lines[0].startswith('<?xml'):
    formatted = '\n'.join(lines)
print(formatted)

# Using lxml for more control
from lxml import etree

root = etree.fromstring(ugly.encode())
formatted = etree.tostring(root, pretty_print=True, xml_declaration=True,
                           encoding='UTF-8').decode()
print(formatted)

# Validate XML well-formedness
def is_valid_xml(text):
    try:
        etree.fromstring(text.encode())
        return True, None
    except etree.XMLSyntaxError as e:
        return False, str(e)

valid, error = is_valid_xml('<root><unclosed>')
print(f'Valid: {valid}, Error: {error}')

Bash (command-line tools)

# Format XML with xmllint (part of libxml2)
xmllint --format input.xml > formatted.xml

# Format and validate
xmllint --format --schema schema.xsd input.xml

# Minify XML
xmllint --noblanks input.xml > minified.xml

# Format XML from pipe (e.g., curl response)
curl -s https://api.example.com/data.xml | xmllint --format -

# Using Python one-liner
python3 -c "import xml.dom.minidom,sys; print(xml.dom.minidom.parseString(sys.stdin.read()).toprettyxml(indent='  '))" < input.xml

# Validate well-formedness only
xmllint --noout input.xml && echo "Valid" || echo "Invalid"

Real-World Use Cases

SOAP API debugging. Enterprise SOAP APIs return XML responses that are often minified or poorly formatted. Paste the response into the XML Formatter to understand the element hierarchy, find the data you need, and verify namespace prefixes are correct.

Android layout development. Android layout XML files get complex with nested LinearLayouts, ConstraintLayouts, and custom views. Proper formatting reveals the view hierarchy and makes it easier to spot misconfigured attributes or redundant nesting.

RSS and Atom feed inspection. When debugging RSS feed generation or parsing, formatted XML shows the channel structure, item elements, and metadata clearly. This helps verify that feed readers will parse your output correctly.

Configuration management. Maven pom.xml, Spring XML configs, .csproj files, and other XML-based configuration formats benefit from consistent formatting. When comparing config files across environments, format both with the XML Formatter, then use the Text Diff to find differences.

Common Mistakes and Gotchas

XML is case-sensitive, unlike HTML. <Book> and <book> are different elements. This catches developers who are used to HTML's case-insensitivity. Always match the exact case of element and attribute names.

Every tag must be closed. <br> is valid HTML5 but invalid XML — it must be <br/>. Similarly, <img src="photo.jpg"> must be <img src="photo.jpg"/>. Forgetting to close tags is the most common XML well-formedness error.

Ampersands must be escaped. The character & in XML content must be written as &amp;. URLs with query strings (?a=1&b=2) break XML parsers unless the ampersand is escaped (?a=1&amp;b=2). CDATA sections (<![CDATA[...]]>) are an alternative for content with many special characters.

Namespace prefix mismatches cause silent failures. If an element uses prefix ns1: but the namespace declaration is xmlns:soap=, the parser treats them as different namespaces. Always verify that prefixes match their declarations.

Frequently Asked Questions

When should I use XML instead of JSON? Use XML when your system requires it (SOAP APIs, legacy integrations, Android layouts, RSS feeds), when you need document validation against a schema (XSD), when you need mixed content (text with inline markup), or when your industry standard mandates it (healthcare HL7, finance FIX). For new web APIs, JSON is simpler.

How do I convert XML to JSON? Use xml2js in JavaScript, xmltodict in Python, or encoding/xml in Go. Parse the XML first, then serialize to JSON. Format the resulting JSON with the JSON Formatter. Be aware that XML attributes, namespaces, and mixed content do not map cleanly to JSON.

What is the difference between well-formed and valid XML? Well-formed XML follows the basic syntax rules (matched tags, quoted attributes, single root element). Valid XML additionally conforms to a schema (DTD or XSD) that defines which elements and attributes are allowed. The XML Formatter checks well-formedness; schema validation requires a separate tool.

Conclusion

XML formatting and validation remain essential skills for anyone working with enterprise APIs, configuration files, or data interchange. Properly formatted XML reveals structure, catches syntax errors, and makes debugging significantly faster.

The FlipMyCase XML Formatter handles formatting, validation, and minification instantly in your browser. For programmatic formatting, use xmllint on the command line, xml.dom.minidom in Python, or xml-formatter in JavaScript. Compare XML files with the Text Diff tool and format related formats with the JSON Formatter and YAML Formatter.

📄 Try XML Formatter

Free, no signup, runs in your browser. Try it now.

Frequently Asked Questions

How do I format XML online?

Paste your XML into the FlipMyCase XML Formatter. It instantly adds proper indentation, aligns nested elements, and validates well-formedness. Works with SOAP, RSS, SVG, config files, and any standard XML.

What is the difference between XML and HTML formatting?

XML is stricter than HTML — every tag must be closed, attribute values must be quoted, and tags are case-sensitive. The FlipMyCase XML Formatter enforces these rules and flags violations that an HTML formatter would ignore.

Can I convert XML to JSON?

Not directly in this tool, but you can format the XML to understand its structure, then use a programming library like xml2js (JavaScript) or xmltodict (Python) to convert it to JSON. Format the result with the JSON Formatter.

Does XML formatting affect data?

No. XML formatting only changes whitespace between tags, which is insignificant in most XML applications. The exception is mixed-content elements where text and tags are interleaved — good formatters preserve this.

Free Text Tools