XMLSerializer

Create elements with javascript, apply some conditions, and display them in the console using XMLSerializer.

The XMLSerializer is a JavaScript object that is commonly used in web development to convert JavaScript objects or values to XML format and vice versa.

Example:

var node = document.createElement("Devicea"); // do not use the methods provided by the document namespace 

node.setAttribute('ID', 1000);

node.setAttribute('DESCRIPTION', 'NameA');

document.body.appendChild(node); // add the element

if(document.getElementById("1000").textContent == '') {

var node1 = document.createElement("Deviceb"); // do not use the methods provided by the document namespace 

node1.setAttribute('ID', 2000);

node1.setAttribute('DESCRIPTION', 'Nameb');

document.getElementsByTagName("devicea")[0].appendChild(node1); // add the element

} else {

  console.log("not updated");

}

var serializer = new XMLSerializer();

var xmlString = serializer.serializeToString(node);

console.log('xmlString :' + xmlString);

In this example, xmlString is an XML-formatted string that is converted to a DOM document using the DOMParser. Then you can use the values from the DOM document to create a JavaScript object.

It's worth noting that while the XMLSerializer and DOMParser are commonly used in web development when dealing with XML, they may not be as useful in modern applications that frequently use JSON for data interchange. When working with JSON, you'd usually use JSON.stringify and JSON.parse instead.

Download source code from Github Link:

https://github.com/kanakaraju/JSON_Easy_Tricks/blob/main/xm_create.html

Comments

Popular posts from this blog

Three.js 3D Graphics Library

NVD3 is a JavaScript library that extends the D3.js library

HTML Marquee