Skip to content

Instantly share code, notes, and snippets.

@miroslavradojevic
Created March 9, 2018 12:02
Show Gist options
  • Save miroslavradojevic/3110cbb9b69f2d6f7c1cfcba8630bb31 to your computer and use it in GitHub Desktop.
Save miroslavradojevic/3110cbb9b69f2d6f7c1cfcba8630bb31 to your computer and use it in GitHub Desktop.
Create XML in JavaScript
var xmlDoc = document.implementation.createDocument(null, "devices");
var elements = xmlDoc.getElementsByTagName("devices");
var node = xmlDoc.createElement("DeviceA"); // do not use the methods provided by the document namespace
node.setAttribute('ID', 1000);
node.setAttribute('DESCRIPTION', 'NameA');
elements[0].appendChild(node); // add the element
var node = xmlDoc.createElement("DeviceB");
node.setAttribute('ID', 2000);
node.setAttribute('DESCRIPTION', 'NameB');
elements[0].appendChild(node); // add the element
var serializer = new XMLSerializer();
var xmlString = serializer.serializeToString(xmlDoc);
console.log('xmlString :\n' + xmlString);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment