Web Developmentbeginner10 min read

XML vs JSON: Understanding Data Formats

Compare XML and JSON, understand XML syntax, learn XPath for querying XML data, and discover when XML still has the advantage over JSON.

XML's Rise and JSON's Takeover

In the early 2000s, XML (eXtensible Markup Language) dominated data interchange. SOAP web services, RSS feeds, enterprise middleware, and configuration systems all used XML. It came with a rich ecosystem: XSD for schema validation, XSLT for transformation, XPath for querying, and mature tooling.

When AJAX and REST APIs emerged, JSON (JavaScript Object Notation) quickly displaced XML for most web use cases. JSON is simpler, smaller, and directly consumable by JavaScript without parsing overhead.

Today XML remains dominant in specific domains: Microsoft Office documents (DOCX, XLSX are zipped XML), SVG graphics, RSS/Atom feeds, SAML authentication, Android layouts, and most enterprise Java ecosystems.

XML Syntax Fundamentals

XML uses a tag-based structure similar to HTML:

  • Elements — the primary building block: `<user>Alice</user>`
  • Attributes — key-value pairs on opening tags: `<user id="1" role="admin">`
  • Text content — the value between opening and closing tags
  • CDATA sections — raw text that won't be parsed as XML: `<![CDATA[<b>not a tag</b>]]>`
  • Namespaces — prevent name collisions in mixed documents: `<ns:user xmlns:ns="http://example.com">`
  • XML Declaration — optional first line specifying version and encoding
<?xml version="1.0" encoding="UTF-8"?>
<users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <user id="1">
    <name>Alice</name>
    <email>alice@example.com</email>
    <roles>
      <role>admin</role>
      <role>editor</role>
    </roles>
  </user>
</users>

When XML Has Advantages

XML genuinely outperforms JSON in certain scenarios:

  • Schema validation — XSD (XML Schema Definition) provides rigorous type validation, constraints, and documentation that JSON Schema can't fully match in enterprise contexts
  • Mixed content — embedding markup in text (like HTML does) is natural in XML; JSON can only store plain strings
  • Transformation — XSLT (XSL Transformations) can transform XML into HTML, PDF, or other XML formats with zero code
  • Mature enterprise tooling — decades of XML tooling in Java (JAXB, SAX, DOM) and .NET
  • Document-oriented data — books, legal documents, and publishing workflows use DocBook and DITA XML standards

XPath: Querying XML Data

XPath is a query language for selecting nodes from XML documents. It uses a path notation similar to filesystem paths:

<!-- Given this XML -->
<bookstore>
  <book category="fiction">
    <title>The Great Gatsby</title>
    <price>12.99</price>
  </book>
  <book category="tech">
    <title>Clean Code</title>
    <price>35.00</price>
  </book>
</bookstore>

<!-- XPath expressions -->
/bookstore/book/title          <!-- all title elements -->
/bookstore/book[1]/title       <!-- first book's title -->
/bookstore/book[@category='tech']/title   <!-- tech books -->
/bookstore/book[price>20]/title           <!-- books over $20 -->
//price                        <!-- all price elements anywhere -->
count(/bookstore/book)         <!-- number of books: 2 -->

Converting Between XML and JSON

XML and JSON have fundamentally different data models, so conversion is never perfectly lossless:

  • Attributes vs keys — XML attributes (`<user id="1">`) have no direct JSON equivalent; converters typically promote them to keys: `{"@id": "1"}`
  • Repeated elements — `<role>admin</role><role>editor</role>` maps to a JSON array, but a single `<role>admin</role>` might map to a string — inconsistent without a schema
  • Mixed content — text nodes mixed with element children don't map cleanly to JSON

Despite these limitations, conversion is useful for migrating between API formats, importing XML data feeds into JSON-based systems, or exploring XML structure in a familiar format.

Working with XML Data on DevForge

The DevForge XML Formatter pretty-prints and validates XML structure — useful for making minified or poorly indented XML readable before analysis.

The XPath Tester lets you write and test XPath expressions against any XML document without writing code, making it practical for quick data extraction or exploring an unfamiliar XML schema.

The XML to JSON and JSON to XML converters handle the format migration for you, with sensible defaults for handling the structural mismatches between the two formats.

Try it on DevForge

Free online tools related to this tutorial — no signup required.

Related Tutorials