XML Formatter & Validator

Format, validate, and analyze XML documents with comprehensive options

Input & Settings

2 spaces

Formatted XML

Formatted XML will appear here

Analysis & Stats

Validation statistics will appear here

• Well-formed: Proper tag nesting and closing

• Valid: Conforms to DTD/Schema rules

• Case-sensitive: Tags and attributes

• Single root: One top-level element

XML Examples & Use Cases

Simple Structure

Basic XML with elements and text content.

<person>\n  <name>John Doe</name>\n  <age>30</age>\n  <active>true</active>\n</person>

With Attributes

XML elements with attributes for metadata.

<book id="123" category="fiction">\n  <title lang="en">Sample Book</title>\n  <author nationality="US">John Author</author>\n</book>

Details

This tool helps you format and validate XML (Extensible Markup Language) documents. XML is widely used for storing and transporting data, especially in configuration files, web services (SOAP), and data exchange between systems. The formatter makes your XML human-readable with proper indentation, while the validator checks for well-formedness and can validate against XML schemas.

Examples

Basic XML Formatting

<!-- Before formatting -->
<root><element attribute="value">Text</element><element>More text with <child>nested element</child></element></root>

<!-- After formatting -->
<root>
  <element attribute="value">Text</element>
  <element>
    More text with 
    <child>nested element</child>
  </element>
</root>

Transforms a compact, hard-to-read XML document into a properly indented format with clear hierarchical structure.

XML Validation

<!-- Invalid XML (Missing closing tag) -->
<person>
  <name>John Doe</name>
  <age>30</age>
  <email>john@example.com
</person>

<!-- Error Message -->
Error at line 5: Element 'email' is not closed

Validation detects common XML errors such as missing closing tags, invalid nesting, or malformed syntax.

XML with Namespaces

<!-- Before formatting -->
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"><soap:Header><auth xmlns="http://example.org/auth"><username>user</username><password>pass</password></auth></soap:Header><soap:Body><m:GetStockPrice xmlns:m="http://example.org/stock"><m:StockName>IBM</m:StockName></m:GetStockPrice></soap:Body></soap:Envelope>

<!-- After formatting -->
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/">
  <soap:Header>
    <auth xmlns="http://example.org/auth">
      <username>user</username>
      <password>pass</password>
    </auth>
  </soap:Header>
  <soap:Body>
    <m:GetStockPrice xmlns:m="http://example.org/stock">
      <m:StockName>IBM</m:StockName>
    </m:GetStockPrice>
  </soap:Body>
</soap:Envelope>

Formats complex XML with namespaces, common in SOAP web services and enterprise applications.

    Examples

    Format a simple person document

    Input
    <person><name>John Doe</name><age>30</age><active>true</active></person>
    Output
    <person>
      <name>John Doe</name>
      <age>30</age>
      <active>true</active>
    </person>
    

    With the default two-space indentation, each child element is rebuilt on its own line and the result ends with a newline.

    Format elements with attributes

    Input
    <book id="123" category="fiction"><title lang="en">The Great Gatsby</title><year>1925</year></book>
    Output
    <book id="123" category="fiction">
      <title lang="en">The Great Gatsby</title>
      <year>1925</year>
    </book>
    

    Attributes are retained while nested elements are expanded using the configured indentation.

    About this tool

    The XML Formatter & Validator parses XML with fast-xml-parser and rebuilds it as indented XML. The default configuration keeps attributes, converts recognizable tag and attribute values, trims surrounding text whitespace, and formats output with two spaces per level; the indent slider supports one through eight spaces.

    Alongside the formatted document, the tool reports whether parsing succeeded, counts parsed element keys and attributes, records the source line count and processing time, and warns about a likely unescaped ampersand or content before the root element. You can also validate without producing formatted output.

    Input can be pasted, restored from saved tool input, loaded from the built-in samples, or uploaded from an .xml or .txt file. Successful output can be copied or downloaded as an XML file. The formatter rebuilds a parsed object rather than preserving the source byte-for-byte, so lexical details such as original whitespace may change.

    How to use

    1. Load XML

      Paste XML, upload an .xml or .txt file, or choose one of the Simple, Attributes, Nested, or CDATA samples.

    2. Choose effective formatting options

      Set the indent width and decide whether text values should preserve surrounding whitespace or whether empty nodes should be emitted in compact form.

    3. Format or validate

      Click Format & Validate to rebuild indented XML, or Validate Only to inspect parsing and statistics without creating output.

    4. Review and export

      Check errors, warnings, element and attribute counts, then copy the output or download it as an .xml file.

    Use cases

    Review a compact API payload

    Expand a one-line XML response into an indented document that is easier to inspect during integration debugging.

    Check basic well-formedness

    Run Validate Only to catch parser errors and review warnings before passing XML to another system.

    Inspect document structure

    Use the element, attribute, line, error, and warning counts for a quick overview of an unfamiliar XML file.

    Prepare readable configuration

    Format XML-based configuration before review, then copy or download the rebuilt document.

    Common mistakes

    Mistake:Treating a successful parse as validation against a DTD or XML Schema.

    Fix:Use the result as a well-formedness check only; the component does not load or enforce a DTD or XSD.

    Mistake:Expecting the formatter to preserve every lexical detail.

    Fix:Keep the original file when comments, CDATA boundaries, entity spelling, attribute order, or exact whitespace must remain byte-for-byte unchanged.

    Mistake:Assuming every visible switch changes the parser or builder.

    Fix:Only rely on behavior visible in the result. In the current component, preserve-comments, sort-attributes, and self-closing-tags state is not passed into the parser or builder options.

    Mistake:Reading the element statistic as a standards-level node count.

    Fix:Treat it as the component's recursive object-key count; repeated arrays and parser conversions can make it differ from a DOM element count.

    Frequently asked questions

    References & standards