Thursday, January 19, 2012

Converting String to XML in JavaScript

Sometime we need to convert a String in xml form,here is the JavaScript code


<script language=javascript>
//xml data as a string
function fun1()
{
var v="<employee><ename>suraj</ename></employee>";
var xmlForm= StringtoXML(v);
//xmlForm has data in XML format
}

function StringtoXML(text)
{
                if (window.ActiveXObject)
                 {
                  var doc=new ActiveXObject('Microsoft.XMLDOM');
                  doc.async='false';
                  doc.loadXML(text);
                }
              else {
                  var parser=new DOMParser();
                  var doc=parser.parseFromString(text,'text/xml');
                }
                return doc;
            }
 </script>

No comments:

Post a Comment