Created
March 9, 2018 12:02
-
-
Save miroslavradojevic/3110cbb9b69f2d6f7c1cfcba8630bb31 to your computer and use it in GitHub Desktop.
Create XML in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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