简体   繁体   中英

How do I serialize a DOM to XML text, using JavaScript, in a cross browser way?

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)?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM