Stop 01 · Internet & Backend BasicsTopics · 11/15

XML

Part of the Backend Developer Roadmap

In short

JSON won most API battles, but XML never left. Bank integrations, RSS feeds, sitemaps and older SOAP services still use it, and sooner or later you'll have to parse some.

An older, verbose data-interchange format still used in enterprise systems, SOAP APIs, and some legacy integrations — largely superseded by JSON for new APIs.

The mascot ties the ribbon of a thick office file, with smaller folders tucked inside bigger ones.

Think of it like this

The sarkari file with labelled covers

In a government office, every paper sits inside a cover with its name written at the top and bottom. Covers go inside bigger covers. Slow to read, but nothing is unclear. XML wraps every value in an opening and closing tag the same way.

Watch it happen

The same order in two formats

JSON

  • Short: each key appears once
  • Parsed natively by JavaScript
  • Has numbers, booleans and null

XML

  • Verbose: every tag opens and closes
  • Supports attributes and namespaces
  • Everything is text until you convert it

Pick JSON for new APIs. Learn enough XML to work with the systems that still use it: SOAP, RSS, sitemaps and many enterprise integrations.

The big ideas

What you need to know

Elements and attributes

<item qty="2">Notebook</item> has an element (item), an attribute (qty) and text content (Notebook).

It must be well-formed

One root element, every tag closed, tags closed in the right order. One mistake and parsers reject the whole document.

Everything is text

XML has no numbers or booleans. "42" stays a string unless you or your parser convert it.

Watch out for XXE

Some parsers can be tricked into reading server files through 'external entities'. Use a parser that doesn't resolve them.

See it in code

Walk through the code

js
1import { XMLParser } from "fast-xml-parser";2 3const xml = `4<order id="42">5  <item qty="2">Notebook</item>6  <paid>true</paid>7</order>`;8 9const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "" });10const data = parser.parse(xml);11 12console.log(data.order.id);             // "42"13console.log(data.order.item.qty);       // "2"14console.log(data.order.item["#text"]);  // "Notebook"

1/3An order with an attribute (id), a child with its own attribute (qty) and a plain child (paid).

fast-xml-parser is a popular npm package. Attribute values stay strings here, so convert id and qty with Number() before doing any maths.

Save yourself the pain

Common mistakes

Oops

Building XML by gluing strings, like "<name>" + userInput + "</name>".

Do this instead

A name like 'Tom & Jerry' breaks the document. Use a builder library, or escape &, < and > properly.

Oops

Assuming a tag that appears once is an object and twice is an array.

Do this instead

Many parsers change shape based on count. Use the parser's isArray option for tags that can repeat.

Your mission

Read a real sitemap

~20 min

Quick check

Which of these is NOT well-formed XML?

Go deeper

  1. 1Compare the same data structure written in XML vs JSON.
  2. 2Learn where XML still shows up today: SOAP, RSS feeds, some enterprise/banking APIs.
  3. 3Understand why JSON won out for most modern web APIs — smaller, easier to parse.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge