XML
eXtensible Markup Language
Extensible markup language. A data interchange format. Losing ground to JSON.
eXtensible Markup Language
Extensible markup language. A data interchange format. Losing ground to JSON.
XML (eXtensible Markup Language) is a markup language for storing and transmitting data. It was standardized by the W3C in 1998, and unlike HTML, it is "extensible" because users can define their own tags.
XML is self-descriptive, so the meaning of the data can be understood just from the tag names. It is also independent of platform and language, so it has long been used for data exchange between systems.
Although JSON is mainly used in current web APIs, XML is still actively used in SOAP, RSS, SVG, and configuration files (pom.xml, AndroidManifest.xml).
Basic XML Document
<?xml version="1.0" encoding="UTF-8"?> <users> <user id="1"> <name>Hong Gil-dong</name> <email>hong@example.com</email> <roles> <role>admin</role> <role>editor</role> </roles> </user> <user id="2"> <name>Kim Cheol-su</name> <email>kim@example.com</email> </user> </users>
Parsing XML in JavaScript
// XML ๋ฌธ์์ด ํ์ฑ
const xmlString = `<book><title>JavaScript</title></book>`;
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
// ๋ฐ์ดํฐ ์ ๊ทผ
const title = xmlDoc.querySelector('title').textContent;
console.log(title); // "JavaScript"
// XPath๋ก ๊ฒ์
const result = xmlDoc.evaluate(
'//book/title',
xmlDoc,
null,
XPathResult.STRING_TYPE
);
console.log(result.stringValue);
XML vs JSON Comparison
// XML (61 bytes) <user> <name>Hong Gil-dong</name> <age>30</age> </user> // JSON (35 bytes) - more concise { "name": "Hong Gil-dong", "age": 30 }
Junior Developer
"Everyone uses JSON these days, so why do I need to learn XML?"
Senior
"It's essential when integrating with legacy systems or SOAP APIs. Financial institutions and public agencies in particular still use XML a lot in their APIs. Maven's pom.xml and Android layouts are XML too."
Junior Developer
"Oh, so that's why the configuration files are XML!"
Well-formed Rules
XML is stricter than HTML: it is case-sensitive, requires every tag to be closed, and requires attribute values to be quoted. Violating these rules causes parsing errors.
Beware of XXE Attacks
It may be vulnerable to XXE (XML External Entity) attacks through external entities. When parsing untrusted XML, external entities must be disabled.
Size and Parsing Performance
Compared to JSON, the overhead from tags is larger. Performance impact should be considered when transmitting large volumes of data.