XML Formatter — Format, Validate, and Pretty Print XML
XML (eXtensible Markup Language) is used for configuration files, API responses, data exchange, RSS feeds, SOAP web services, and document storage. XML sent over a network or stored in files is often minified — all on one line, impossible to read. The XML formatter on PublicSoftTools instantly formats XML into indented, human-readable output and validates the structure for errors.
How to Format XML
- Open the XML formatter.
- Paste your XML into the input panel — minified, single-line XML works fine.
- Click Format. The formatter parses the XML and outputs it with consistent indentation (2 or 4 spaces, selectable).
- Any XML errors are highlighted with the line and character position, and a description of the problem.
- Use Minify to remove all unnecessary whitespace and produce compact XML for transmission.
- Copy the formatted or minified output.
XML Structure Reference
| Element type | Syntax | Purpose |
|---|---|---|
| Declaration | <?xml version="1.0" encoding="UTF-8"?> | Optional first line of XML document. Specifies XML version and character encoding. |
| Element (tag) | <tagName>content</tagName> | The fundamental unit of XML. Has a start tag, content, and end tag. Case-sensitive. |
| Attribute | <element attr="value"> | Name-value pair within a start tag. Values must be quoted (single or double). |
| Self-closing element | <element /> | Element with no content. Shorthand for <element></element>. |
| Text content | <name>John Smith</name> | Character data between start and end tags. The actual content of an element. |
| Comment | <!-- This is a comment --> | Human-readable notes not processed by parsers. Cannot contain --. |
| CDATA section | <![CDATA[raw text & <tags>]]> | Blocks of text where < and & do not need escaping. Used for embedded code or raw text. |
| Namespace | <ns:element xmlns:ns="uri"> | Avoids element name conflicts when combining XML from different vocabularies (e.g., SOAP+XML). |
Common XML Errors and Fixes
| Error | Example | Fix |
|---|---|---|
| Unclosed tag | <name>John</nam> | Every start tag must have a matching end tag with identical name: </name> |
| Unescaped special characters | Price: 5 < 10 | Escape: < → < > → > & → & " → " ' → ' |
| Wrong case on tag names | <Name>text</name> | XML is case-sensitive — <Name> and <name> are different tags. Must match exactly. |
| Unquoted attribute values | <el attr=value> | All attribute values must be quoted: attr="value" or attr='value' |
| Multiple root elements | <a>x</a><b>y</b> | XML documents must have exactly one root element that wraps all others. |
| Invalid characters in names | <2element> or <el-em> | Element names must start with a letter or underscore, not a digit or hyphen. Hyphens allowed after first character. |
Well-Formed vs. Valid XML
There are two distinct concepts in XML correctness:
- Well-formed XML: Follows the basic XML syntax rules — every start tag has an end tag, attributes are quoted, there is one root element, special characters are escaped, and element names are valid. All XML parsers check this.
- Valid XML: Well-formed AND conforms to a specific schema (DTD, XML Schema/XSD, or RELAX NG). A schema defines which elements and attributes are allowed, their order, data types, and relationships. A document can be well-formed but invalid if it uses the wrong element names or structure for a given schema.
The XML formatter validates well-formedness — it will catch syntax errors. Schema validation requires the schema file (XSD or DTD) alongside the XML, which is a separate tool. For most purposes (APIs, config files, debugging), well-formedness validation is what is needed.
XML Special Characters and Escaping
Five characters have special meaning in XML and must be escaped when they appear in text content or attribute values:
<for < (less than) — starts a tag>for > (greater than) — ends a tag&for & (ampersand) — starts an entity reference"for " (double quote) — inside attribute values with double quotes'for ' (single quote/apostrophe) — inside attribute values with single quotes
A CDATA section (<![CDATA[...]]>) allows any content without escaping — useful for embedding JavaScript, HTML, or SQL inside XML without converting every < and &.
XML in APIs: REST vs. SOAP
XML was the dominant data format for web APIs throughout the 2000s, particularly in SOAP (Simple Object Access Protocol) web services. It has largely been replaced by JSON for REST APIs due to JSON's more compact syntax and native JavaScript support. However, XML is still widely used:
- SOAP APIs: Enterprise software, banking, healthcare, and government systems commonly use SOAP over HTTP with XML payloads — these systems are often decades old and changing the data format is not practical.
- RSS and Atom feeds: News, podcast, and blog feeds are XML-based.
- Office documents: .docx, .xlsx, .pptx files are ZIP archives containing XML files.
- SVG images: SVG is XML-based — scalable vector graphics can be embedded in HTML or edited as text.
- Android layouts: Android app UI is defined in XML layout files.
- Maven/Gradle (Java): pom.xml files define Java project structure and dependencies.
- Sitemap.xml: Search engine sitemaps are XML files listing URLs.
XML vs. JSON: When to Use Each
Choosing between XML and JSON depends on the use case:
- Use XML when: the system expects it (SOAP APIs, existing integrations), documents need schemas (XSD validation), mixed content (text with embedded tags) is required, or namespaces are needed.
- Use JSON when: building a new REST API, working with JavaScript front-ends, minimising payload size, or readability and simplicity are priorities. JSON is natively parseable in JavaScript; XML requires a parser.
- Both are acceptable when: configuring tools (Maven uses XML; many others use JSON or YAML), serving data feeds, or integrating with existing systems that accept either format.
XPath: Querying XML
XPath is a query language for selecting nodes in XML documents. It is to XML what CSS selectors are to HTML. Common XPath expressions:
//element— selects all elements with that name anywhere in the document/root/parent/child— absolute path from root//element[@attr="value"]— element with specific attribute value//element[1]— first element in its parent (XPath uses 1-based indexing)//element/text()— text content of an element
XPath is used in XSLT (transforming XML to other formats), test automation (Selenium locators), and XML processing libraries.
Common Questions
What does well-formed XML mean in practice?
Well-formed XML means a parser can read it without errors. The key rules: one root element wrapping everything; all tags are closed (either <tag>...</tag> or <tag />); tags are properly nested (no overlapping — <a><b></a></b> is invalid); attribute values are quoted; five special characters are escaped. If an XML document is not well-formed, XML parsers throw an error and refuse to process it.
Can XML have mixed content?
Yes — XML supports mixed content: elements containing both text and child elements. Example: <p>This is <em>important</em> text.</p> — the <p> element contains both a text node and an <em> child element. This is common in document-centric XML (HTML-like content). Schema definitions must explicitly allow mixed content in the element declaration.
What is the difference between XML and HTML?
HTML (HyperText Markup Language) and XML look similar but serve different purposes and have different rules. HTML has fixed tags with defined meanings (<p>, <div>, <img>); XML tags are custom (you define them). HTML browsers are lenient — they attempt to display malformed HTML. XML parsers are strict — malformed XML is an error. XHTML was an attempt to make HTML conform to XML rules. Modern HTML5 and XML coexist as separate standards; they are not versions of the same language.
Format Your XML
Paste any XML — minified or malformed — and get instant formatted, indented output with error highlighting. Free, no signup.
Open XML Formatter