Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 204
Eero Avatar asked Sep 04 '08 10:09

Eero


People also ask

What is XML serialization?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

Is a Namespsace for XML serialization?

Xml. Serialization namespace contains several Attribute classes that can be applied to members of a class. For example, if a class contains a member that will be serialized as an XML element, you can apply the XmlElementAttribute attribute to the member.

What is SerializeToString?

Parsing and Serialization SerializeToString() : serializes the message and returns it as a string. Note that the bytes are binary, not text; we only use the str type as a convenient container. ParseFromString(data) : parses a message from the given string.

What is XML DOM how Dom parses the XML file?

The XML Document Object Model (DOM) class is an in-memory representation of an XML document. The DOM allows you to programmatically read, manipulate, and modify an XML document. The XmlReader class also reads XML; however, it provides non-cached, forward-only, read-only access.


1 Answers

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.

like image 130
Huppie Avatar answered Oct 20 '22 21:10

Huppie