正在加载,请稍候…

XML Formatter: How to Pretty-Print and Validate XML

Format and validate XML documents online. Learn XML structure, namespaces, attributes, and common parsing errors. Pretty-print minified XML with syntax highlighting and error detection.

What Is XML?

XML (Extensible Markup Language) is a markup language designed to store and transport data in a format that is both human-readable and machine-readable. Unlike HTML (which defines a fixed set of tags), XML allows you to define your own tags, making it highly flexible for representing any structured data.

XML was developed by the W3C in 1998 and remains widely used in enterprise systems, SOAP web services, document formats (DOCX, SVG, RSS), and configuration files (Maven, Spring, Android).

XML vs HTML: Key Differences

Feature XML HTML
Purpose Data storage and transport Web page structure
Tag names Custom, user-defined Fixed (div, p, span...)
Case sensitivity Case-sensitive Case-insensitive
Closing tags Mandatory Optional for some
Self-closing Required for empty elements Optional
Error handling Strict (malformed = invalid) Lenient (browsers guess)

XML Syntax Rules

XML has strict syntax requirements:

  1. Exactly one root element: Every XML document has one root element that contains all others.

  2. Tags are case-sensitive: <Name> and <name> are different elements.

  3. All tags must be closed: Either <tag></tag> or self-closing <tag />.

  4. Proper nesting: Tags must be properly nested (no overlapping):

    Bad:  <b><i>text</b></i>
    Good: <b><i>text</i></b>
    
  5. Attribute values must be quoted: <element attr="value"> or single quotes.

  6. Special characters must be escaped:

    &  ->  &amp;
    <  ->  &lt;
    >  ->  &gt;
    "  ->  &quot;
    '  ->  &apos;
    

Well-Formed vs Valid XML

Well-formed XML follows all syntax rules. Any XML parser will accept it.

Valid XML additionally conforms to a schema (DTD or XSD). Validity is optional but important for data exchange.

XML Structure Example

<?xml version="1.0" encoding="UTF-8"?>
<library xmlns="http://example.com/library">
  <book id="1" category="fiction">
    <title>The Great Gatsby</title>
    <author>F. Scott Fitzgerald</author>
    <year>1925</year>
    <price currency="USD">12.99</price>
  </book>
  <book id="2" category="technical">
    <title>Clean Code</title>
    <author>Robert C. Martin</author>
    <year>2008</year>
    <price currency="USD">35.99</price>
  </book>
</library>

Namespaces

XML namespaces prevent name conflicts when combining XML from different sources:

<root
  xmlns:html="http://www.w3.org/1999/xhtml"
  xmlns:svg="http://www.w3.org/2000/svg">
  
  <html:div>HTML content</html:div>
  <svg:circle cx="50" cy="50" r="10"/>
</root>

XML in Modern Development

While JSON has largely replaced XML for REST APIs, XML remains essential in several contexts:

  • SOAP web services: Still common in enterprise environments (banking, healthcare)
  • Office documents: DOCX, XLSX, PPTX are ZIP archives containing XML files
  • SVG graphics: Scalable Vector Graphics is XML-based
  • RSS/Atom feeds: Web content syndication uses XML
  • Android: Layout files, AndroidManifest.xml
  • Maven/Gradle: Java build configuration
  • Spring: Application context and configuration
  • XSLT: XML transformation language for converting XML to other formats

XPath: Querying XML

XPath is a query language for selecting nodes in XML:

/library/book[@category='fiction']    # Select fiction books
/library/book/title                   # Select all titles
//book[price > 20]                    # Books costing over 20 (anywhere in document)
/library/book[last()]                 # Last book
count(/library/book)                  # Count all books

Using This Tool

Paste any XML document to instantly format and prettify it with proper indentation and syntax highlighting. The tool also validates XML syntax and reports specific errors with line numbers.

-> Try the XML Formatter