28

I have an XML object (loaded using XMLHTTPRequest's responseXML). I have modified the object (using jQuery) and would like to store it as text in a string.

There is apparently a simple way to do it in Firefox et al:

var xmlString = new XMLSerializer().serializeToString( doc );

(from rosettacode )

But how does one do it in IE6 and other browsers (without, of course, breaking Firefox)?

1 Answer 1

35

You can use doc.xml in internet exlporer.

You'll get something like this:

function xml2Str(xmlNode) {
   try {
      // Gecko- and Webkit-based browsers (Firefox, Chrome), Opera.
      return (new XMLSerializer()).serializeToString(xmlNode);
  }
  catch (e) {
     try {
        // Internet Explorer.
        return xmlNode.xml;
     }
     catch (e) {  
        //Other browsers without XML Serializer
        alert('Xmlserializer not supported');
     }
   }
   return false;
}

Found it here.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks... I finally found this after two days of searching. (It took me a while to realize that .xml was simply not there for FF/Chrome, I had assumed I was doing something wrong.)
Webkit currently has a bug (e.g. in Chrome 19) and will not return correct XML: xmlNode = document.createElement('img'); xmlNode.src = "test.png" xmlNode.alt = "test" (new XMLSerializer()).serializeToString(xmlNode); Returns: "<img src="test.png" alt="test">"
@cburgmer that's not an xml node
@Esailija I don't understand, what is it then? I expect XMLSerializer to serialize xmlNode into <img src… /> (note the trailing slash)
@cburgmer it's a html node, you created it using a html document. XML nodes don't have properties like .src. See what an actual xml element has with: console.dir( document.implementation.createDocument( null, "xml", null ).createElement("img") )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.