0 votes
1 view
in Web Technology by (6.8k points)

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

1 Answer

0 votes
by (13k 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
0 votes
1 answer
asked Feb 25 in Web Technology by Jake (6.8k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...