You are here: Reference > JavaScript > client-side > xml handling > objects > XMLSerializer

XMLSerializer object

Browser support:
9
Provides methods to convert a document or a subtree of a document into text or a byte stream.
Note: The XMLSerializer object is supported in Internet Explorer from version 9.

Syntax:

Methods that return the object:
new XMLSerializer ( )

Possible members:

Methods:
serializeToStream (node, stream, charset)
Returns the contents of the document or a node in the document as a byte stream.

Parameters:

node
Required. Reference to an XML or HTML document or a node in the document to serialize.
stream
Required. Reference to a byte stream object.
charset
Required. String that specifies the character set of the byte stream.

Return value:

This method has no return value.
serializeToString (node)
Returns the contents of the document or a node in the document as a string.
Although the serializeToString method is supported but not implemented in Internet Explorer from version 9, using it raises an exception. See the example below for details.

Parameters:

node
Required. Reference to an XML or HTML document or a node in the document to serialize.

Return value:

Returns a string that represents the contents of the document or a node in the document.

Example HTML code 1:

This example illustrates the use of the serializeToString method for a node in a HTML document:
<head>
    <script type="text/javascript">
        function GetContent () {
            if (window.XMLSerializer) {
                var serializer = new XMLSerializer ();
                var str = serializer.serializeToString (document.body);
                alert (str);
            }
            else {
                alert (document.body.outerHTML);
            }
        }
    </script>
</head>
<body>
    <button onclick="GetContent ();">Get the contents of the body</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the serializeToString method for XML documents:
Code
ajax.js
news.xml
<head>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="text/javascript">
        var httpRequest = null;
        
        function SendRequest () {
            if (!httpRequest) {
                httpRequest = CreateHTTPRequestObject ();   // defined in ajax.js
            }
            if (httpRequest) {          
                    // The requested file must be in the same domain that the page is served from.
                var url = "news.xml";
                httpRequest.open ("GET", url, true);    // async
                httpRequest.onreadystatechange = OnStateChange;
                httpRequest.send (null);
            }
        }

        function OnStateChange () {
            if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
                if (IsRequestSuccessful (httpRequest)) {    // defined in ajax.js
                    DisplayContent ();
                }
                else {
                    alert ("Operation failed.");
                }
            }
        }


        function DisplayContent () {
            var xmlDoc = ParseHTTPResponse (httpRequest);   // defined in ajax.js
            if (!xmlDoc)
                return;

            if (window.XMLSerializer) { // all browsers, except IE before version 9
                var serializer = new XMLSerializer();
                    // the serializeToString method raises an exception in IE9
                try {
                    var str = serializer.serializeToString (xmlDoc.documentElement);
                    alert (str);
                    return;
                }
                catch (e) {
                }
            }

            if ('xml' in xmlDoc) {  // Internet Explorer
                alert (xmlDoc.xml);
                return;
            }

            alert("Cannot display the contents of the XML document as a string.");
        }
    </script>
</head>
<body>
    <button onclick="SendRequest ()">Display XML file content</button>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content