XML Validator & Formatter

Validate, format, minify, and convert XML

XML Input

Output

Formatting Options

About XML Validator & Formatter

A comprehensive browser-based tool for validating, formatting, minifying, and converting XML documents. This tool helps developers work with XML data efficiently by providing real-time validation, multiple formatting options, and powerful XPath query capabilities. All processing happens locally in your browser - no data is sent to any server.

Key Features

  • XML Validation: Real-time syntax validation with detailed error messages including line and column numbers
  • Format XML: Beautify XML with customizable indentation (spaces or tabs, 1-8 size)
  • Minify XML: Compress XML by removing unnecessary whitespace and comments
  • XML to JSON Conversion: Transform XML documents to JSON format while preserving structure
  • XPath Query Testing: Execute and test XPath expressions against your XML documents
  • Statistics: View detailed statistics including element count, attributes, depth, and file size

How to Use

1. Format Mode

Format mode beautifies your XML with proper indentation and structure, making it easier to read and maintain. This is ideal for development, debugging, and code reviews.

Formatting Options:

  • Indent Size: Choose between 1-8 spaces or tabs for indentation
  • Indent Type: Select spaces or tabs based on your coding standards
  • Sort Attributes: Alphabetically sort XML attributes for consistency
  • Remove Comments: Strip out XML comments from the output

Example - Format XML:

Input (Minified):

<?xml version="1.0"?><bookstore><book category="cooking"><title>Italian Recipes</title><author>Giada</author></book></bookstore>

Output (Formatted with 2-space indentation):

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="cooking">
    <title>Italian Recipes</title>
    <author>Giada</author>
  </book>
</bookstore>

2. Minify Mode

Minification reduces XML file size by removing unnecessary whitespace, line breaks, and optionally comments. This is useful for reducing bandwidth and improving transmission speed in APIs and web services.

Example - Minify XML:

Input (Formatted):

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <!-- Product catalog -->
  <product id="123">
    <name>Widget</name>
    <price>19.99</price>
  </product>
</catalog>

Output (Minified with comments removed):

<?xml version="1.0" encoding="UTF-8"?><catalog><product id="123"><name>Widget</name><price>19.99</price></product></catalog>

Result: File size reduced by approximately 30-50% depending on original formatting.

3. Validate Mode (XPath Testing)

Test XPath expressions against your XML document to query and extract specific data. This is essential for working with XML APIs, web scraping, and data extraction.

Common XPath Examples:

  • //book - Select all book elements
  • //book[@category='cooking'] - Select books with category="cooking"
  • //book/title - Select all title elements within book elements
  • //book[price>30] - Select books with price greater than 30
  • //book[1] - Select the first book element
  • //book/title/text() - Get text content of all book titles

Example - XPath Query:

XML Document:

<bookstore>
  <book category="cooking">
    <title>Italian Recipes</title>
    <price>30.00</price>
  </book>
  <book category="children">
    <title>Harry Potter</title>
    <price>29.99</price>
  </book>
</bookstore>

XPath Query: //book[@category='cooking']/title

Result:

Found 1 result(s):

<title>Italian Recipes</title>

4. Convert to JSON Mode

Convert XML documents to JSON format for easier consumption in JavaScript applications and modern APIs. The converter preserves the XML structure, attributes, and text content.

Conversion Rules:

  • XML elements become JSON objects
  • Attributes are stored in an @attributes object
  • Text content is stored in a #text property
  • Multiple child elements with the same name become arrays

Example - XML to JSON:

Input XML:

<person id="123">
  <name>John Doe</name>
  <email>john@example.com</email>
  <phone>555-1234</phone>
  <phone>555-5678</phone>
</person>

Output JSON:

{
  "person": {
    "@attributes": {
      "id": "123"
    },
    "name": "John Doe",
    "email": "john@example.com",
    "phone": [
      "555-1234",
      "555-5678"
    ]
  }
}

Statistics Explained

The tool provides detailed statistics about your XML document:

  • Elements: Total number of XML elements (tags) in the document
  • Attributes: Total count of all attributes across all elements
  • Text Nodes: Number of text content nodes (excluding empty whitespace)
  • Comments: Number of XML comments in the document
  • Depth: Maximum nesting level of elements (tree depth)
  • Size: File size in bytes

Best Practices

  • Validation First: Always validate your XML before processing to catch syntax errors
  • Use Format for Development: Formatted XML is easier to read and debug during development
  • Minify for Production: Use minified XML for APIs and production to reduce bandwidth
  • Test XPath Queries: Use the XPath tester to verify your queries before implementing them in code
  • Keep Backups: Always keep a copy of your original XML before making changes
  • Sort Attributes: Enable attribute sorting for consistent, version-control-friendly XML

Common Use Cases

  • API Development: Format and validate XML responses from SOAP/REST APIs
  • Configuration Files: Beautify XML config files for better readability
  • Data Migration: Convert XML data to JSON for modern applications
  • Web Scraping: Test XPath queries for extracting data from XML feeds
  • File Optimization: Minify XML files before deployment to reduce size
  • Debugging: Format minified XML to understand its structure

Tips & Tricks

  • Use the Load Sample button to quickly test the tool with example data
  • The Upload feature accepts .xml files from your computer
  • Click Download to save processed XML or JSON to your device
  • Use Copy to quickly copy the output to your clipboard
  • All processing is done locally - your data never leaves your browser
  • The tool works offline once the page is loaded

Troubleshooting

Common Errors:

  • "Invalid XML": Check for unclosed tags, missing quotes, or special characters that need escaping
  • "XPath Error": Verify your XPath syntax and ensure it matches your XML structure
  • Empty Output: Make sure you've clicked the "Process" button after entering XML
  • Unexpected Results: Check if "Remove Comments" or "Sort Attributes" options are affecting output

Technical Details

This tool uses the browser's native DOMParser API for XML parsing and validation, ensuring fast and reliable processing. XPath queries are executed using the document.evaluate() method, which supports XPath 1.0 expressions. All operations are performed client-side using modern JavaScript, with no server communication required.