Irreva logo
Explore Irreva
DeveloperApril 5, 2026· 5 min read· Updated June 10, 2026

How to Format and Validate XML Online

Hasanur Rahman

Written by Hasanur Rahman

Founder & Full-Stack Developer · Irreva · Rangpur, Bangladesh

XML (Extensible Markup Language) is used for configuration files, data interchange, RSS feeds, SOAP web services, SVG graphics, and many other purposes. Unformatted XML from an API response or export is nearly unreadable. Formatting it adds indentation and line breaks to make the tag hierarchy visible. This guide covers XML formatting, validation, and common errors.

What XML formatting does

XML formatting adds indentation to nested elements, line breaks between sibling elements, and consistent attribute formatting. A minified XML string that spans one line becomes a tree-structured document where hierarchy is immediately clear.

Beyond formatting, XML validators check that the document is well-formed: all tags are properly opened and closed, attribute values are quoted, and no illegal characters appear in tag names. Well-formedness is a strict requirement — unlike HTML, a browser won't try to correct malformed XML.

The Irreva XML Formatter parses the XML and reformats it with consistent indentation. Validation errors are shown with the line number and a description of the problem.

Common XML errors

Missing closing tags are the most common XML error. Every opening tag must have a corresponding closing tag (or use self-closing syntax: <br/>). Unlike HTML, XML has no optional end tags.

Unescaped special characters cause parsing failures. The characters <, >, &, ', and " have special meaning in XML. To use them in text content or attribute values, use the entity references: &lt;, &gt;, &amp;, &apos;, and &quot;.

Multiple root elements are also invalid. A valid XML document must have exactly one root element that contains all other elements. Two top-level elements without a parent is a well-formedness error.

  • Unclosed tags: <tag> with no </tag>
  • Unescaped < or & in text content
  • Attribute values not quoted
  • Multiple root elements
  • Lowercase/uppercase mismatch: XML is case-sensitive
  • Invalid characters in element names

XML vs JSON — when to use each

JSON has largely replaced XML for new REST APIs because it's less verbose, easier to read, and directly maps to data structures in most languages. But XML remains dominant in several areas: SOAP web services, enterprise integration, Microsoft Office file formats (DOCX, XLSX are zip files containing XML), SVG vector graphics, and Android layouts.

XML has features JSON lacks: XML Schema Definition (XSD) for rigorous data validation, namespaces for combining elements from different vocabularies, XSLT for transforming XML to other formats, and XPath for querying into an XML document.

For configuration files, XML was long the standard (Spring, Maven, web.xml). YAML and JSON have replaced XML in many newer tools, but XML configuration is still common in Java and .NET ecosystems.

Frequently Asked Questions

What is the difference between well-formed and valid XML?

Well-formed XML follows the basic syntax rules: properly nested tags, closed tags, quoted attributes, a single root element. Valid XML additionally conforms to a schema (DTD or XSD) that defines the allowed structure and content. All valid XML must be well-formed, but well-formed XML doesn't have to be valid.

What is a DTD or XSD?

A DTD (Document Type Definition) is an older XML schema language that defines the allowed structure of an XML document. XSD (XML Schema Definition) is the more powerful modern replacement. Both allow you to specify what elements and attributes are allowed, their data types, and their required presence.

Can I use XML in a browser?

Yes. Browsers have built-in XML parsers. SVG is XML and renders natively in all modern browsers. XHTML is XML-based HTML. You can load and query XML with the browser's DOMParser API. However, XML is more verbose than JSON for data fetching, so JSON is preferred for API responses.

What is CDATA in XML?

CDATA (Character Data) sections allow you to include text that contains special characters without escaping them. A CDATA section looks like <![CDATA[ your content here ]]>. Everything inside is treated as literal text, not markup. Useful for embedding code or HTML within XML.

How do I convert XML to JSON?

There are libraries in most languages that handle XML-to-JSON conversion, but the mapping isn't always straightforward — XML's attributes, text nodes, and namespace handling don't have direct JSON equivalents. The xml2js Node.js library and Python's xmltodict are popular options.

Hasanur Rahman

About the author

Hasanur Rahman

Founder & Full-Stack Developer · Irreva · Rangpur, Bangladesh

Hasanur Rahman is the founder of Irreva and a full-stack developer based in Rangpur, Bangladesh. He builds all of Irreva's tools with a focus on privacy-first, browser-based processing.