Convert xml to string with jQuery

Here it is:

<script type='text/javascript'>

function xmlToString(xmlData) { 

    var xmlString;
    //IE
    if (window.ActiveXObject){
        xmlString = xmlData.xml;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else{
        xmlString = (new XMLSerializer()).serializeToString(xmlData);
    }
    return xmlString;
}   

</script>

Taken from here


this works around the .innerHtml problem.

$('<div>').append(xmlObj).html()

This worked for me (credit: http://www.ibm.com/developerworks/xml/tutorials/x-processxmljquerytut/section3.html):

 function getXmlAsString(xmlDom){
      return (typeof XMLSerializer!=="undefined") ? 
           (new window.XMLSerializer()).serializeToString(xmlDom) : 
           xmlDom.xml;
 }          

Here's an example that retrieves information about a column from a SharePoint list:

var soapEnv =
    "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
        <soapenv:Body> \
             <GetList xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                <rowLimit>0</rowLimit> \
                <listName>Announcements</listName> \
            </GetList> \
        </soapenv:Body> \
    </soapenv:Envelope>";


jQuery.support.cors = true; 
$.ajax({
    url: "http://sharepoint/_vti_bin/lists.asmx",
    type: "POST",
    dataType: "xml",
    data: soapEnv,
    contentType: "text/xml; charset=\"utf-8\"",
    complete: function(xData){
        var xmlDoc = $.parseXML(xData.responseText), $xml = $(xmlDoc)           
        $Fields = $xml.find("Field");
        $field = $Fields.filter("Field[Name='Title']")[0];

        //Show the xml
        alert(getXmlAsString( xmlDoc ));
        alert(getXmlAsString( $field ));
    }
});

Spent much time for this problem. With IE 9 above functions should work in another way. Because in IE9 xmlData[0].xml doesn't work (IE still likes jokes). And we must use XMLSerializer with IE v9 and higher (?!)

function xmlToString(xmlData) { // this functions waits jQuery XML 

    var xmlString = undefined;

    if (window.ActiveXObject){
        xmlString = xmlData[0].xml;
    }

    if (xmlString === undefined)
    {
        var oSerializer = new XMLSerializer();
        xmlString = oSerializer.serializeToString(xmlData[0]);
    }

    return xmlString;
}

And example of using it with jQuery 1.8.2 (1.6.4 works too).

   $.ajax(
        {
            type: type,
            url: url,
            data: values,
            dataType: 'html', //get response in plain text
            success: function(response) {    

                //transform it to jQuery XML DOM
                var xmlDoc = jQuery.parseXML(response);
                var xml = $(xmlDoc);

                //do some search and so on
                var divtag = xml.find('div[id="content"]');
                var divtxt = xmlToString(divtag);

                //consume it
                alert(divtxt);
                $('#main-content').html(divtxt);

            }
        });

Had the same problem - xmlString was returning an empty string. Adding [0] to jQuery selector helped to address XML-type object:

Your Javascript:

<script type='text/javascript'>
function xmlToString(xmlData) 
{
    var xmlString;
    //IE
    if (window.ActiveXObject){
        xmlString = xmlData.xml;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else{
        xmlString = (new XMLSerializer()).serializeToString(xmlData[0]);
    }
    return xmlString;
}   
</script>

jQuery:

<script>
$(function(){ 
  xmlData = "<tag>just a sample here</tag>"; 
  xmlData= $(xmlData); 
  if (window.ActiveXObject){ 
    var xmlString = xmlData.xml; 
  } else {
    var oSerializer = new XMLSerializer(); 
    var xmlString = oSerializer.serializeToString(xmlData[0]);
  } 
  console.log(xmlString); 
})
</script>