Before JSON became the favorite, XML was the standard way to send structured data, and you still meet it in feeds, vector graphics, and office documents. Seeing the same data in both formats shows why JSON took over the web, and where XML still earns its place.
{
"student": {
"name": "Ava",
"grade": 10,
"clubs": ["robotics", "chess"]
}
}
<student>
<name>Ava</name>
<grade>10</grade>
<clubs>
<club>robotics</club>
<club>chess</club>
</clubs>
</student>
Both describe the same student. XML marks every piece with an opening and a closing tag, and it has no built-in idea of an array, so the list of clubs becomes a <clubs> element containing several <club> elements.
<tag>content</tag><student id="7">| JSON | XML | |
|---|---|---|
| Size | Shorter | Longer (every value needs two tags) |
| Arrays | Built in: [ ] | Repeat an element |
| Types | string, number, boolean, null | Everything is text |
| Comments | None | Yes: <!-- note --> |
| Attributes | No such thing | Yes, plus namespaces |
| Reading it in JS | JSON.parse, then plain objects | A parser that builds a document tree |
| Typical home | Web APIs, settings, saved data | Feeds, SVG, office files, older services |
const json = '{"name":"Ava","grade":10}';
const xml = "<student><name>Ava</name><grade>10</grade></student>";
console.log(json.length);
console.log(xml.length);
On a big list of records the difference adds up, which matters for loading time on a phone connection.
JSON becomes ordinary objects and arrays with one call, because JSON was modeled on JavaScript values:
const data = JSON.parse('{"name":"Ava","clubs":["robotics","chess"]}');
console.log(data.name);
console.log(data.clubs[1]);
XML needs a parser that builds a document. The browser has one called DOMParser. You then search the document with querySelector and read text with textContent, or an attribute with getAttribute:
const text = `<student id="7"><name>Ava</name><clubs><club>robotics</club><club>chess</club></clubs></student>`;
const doc = new DOMParser().parseFromString(text, "application/xml");
console.log(doc.querySelector("name").textContent);
console.log(doc.querySelector("student").getAttribute("id"));
console.log(doc.querySelectorAll("club").length);
console.log(doc.querySelectorAll("club")[1].textContent);
Notice that the id comes back as the text "7", not the number 7. XML values are always text until you convert them.
Unlike JSON.parse, DOMParser does not throw an error for broken XML. It quietly returns a document that contains a parsererror element, so you have to look for it:
const bad = "<student><name>Ava</student>";
const doc = new DOMParser().parseFromString(bad, "application/xml");
console.log(doc.querySelector("parsererror") !== null);
const good = "<student><name>Ava</name></student>";
const ok = new DOMParser().parseFromString(good, "application/xml");
console.log(ok.querySelector("parsererror") !== null);
JSON.parse does all the workXML is not gone. It suits documents (text mixed with markup), data that needs comments or attributes, and tools built on its strict rules:
Which statement about XML is true?
JSON.parseName two reasons JSON replaced XML for most web APIs.
JSON.parse, and it is easy for people to read.What does this log?
const doc = new DOMParser().parseFromString('<a id="5"></a>', "application/xml");
console.log(typeof doc.querySelector("a").getAttribute("id"));
string. XML attribute values are always text, so "5" is a string. Convert it with Number(...) if you need a number.querySelectorAllparsererror