JavaScript:
Working with XML

How to:

Here’s how to parse XML:

let parser = new DOMParser();
let xmlString = `<note>
                    <to>User</to>
                    <from>Author</from>
                    <heading>Reminder</heading>
                    <body>Don't forget me this weekend!</body>
                 </note>`;

let xmlDoc = parser.parseFromString(xmlString, "application/xml");
console.log(xmlDoc.getElementsByTagName('to')[0].childNodes[0].nodeValue);
// Output: User

And to produce XML:

let xmlDocument = document.implementation.createDocument('', '', null);
let noteElement = xmlDocument.createElement('note');
noteElement.appendChild(xmlDocument.createElement('to')).textContent = 'User';
xmlDocument.appendChild(noteElement);
let serializer = new XMLSerializer();
let xmlString = serializer.serializeToString(xmlDocument);
console.log(xmlString);
// Output: <note><to>User</to></note>

Deep Dive

XML is short for eXtensible Markup Language, a data format that’s been around since the late 90s. It defines a set of rules for encoding documents that both humans and machines can read. Historically, XML gained traction for its flexibility and structured hierarchy, making it a choice for web services, such as SOAP, and numerous configuration files.

Alternatives to XML include JSON (JavaScript Object Notation), which has grown popular for its ease of use with JavaScript and lighter weight. YAML is another alternative, valued for being both human-friendly and a common choice for configuration.

XML is implemented in JavaScript using the DOMParser and XMLSerializer interfaces. The XML DOM (Document Object Model) allows for navigating and editing XML documents just like you would with HTML. Despite JSON’s ascent, understanding XML is key, as numerous legacy systems and specific industries still rely on it for data exchange.

See Also