Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

How to convert XML to JSON and JSON back to  XML in Javascript ?

1 Answer

0 votes
by (13.1k points)

You can do it like this:

function parseXml(xml) {

   var dom = null;

   if (window.DOMParser) {

      try { 

         dom = (new DOMParser()).parseFromString(xml, "text/xml"); 

      } 

      catch (e) { dom = null; }

   }

   else if (window.ActiveXObject) {

      try {

         dom = new ActiveXObject('Microsoft.XMLDOM');

         dom.async = false;

         if (!dom.loadXML(xml)) // parse error ..

            window.alert(dom.parseError.reason + dom.parseError.srcText);

      } 

      catch (e) { dom = null; }

   }

   else

      alert("cannot parse xml string!");

   return dom;

}

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 15, 2021 in Web Technology by Jake (7k points)
0 votes
1 answer
asked Feb 25, 2021 in Web Technology by Jake (7k points)
0 votes
1 answer

Browse Categories

...