XMLSerializer

  • Revision slug: XMLSerializer
  • Revision title: XMLSerializer
  • Revision id: 351259
  • Created:
  • Creator: istos
  • Is current revision? No
  • Comment Added a useful and practical example with description. It presents a reason as to why you might need to use XMLSerializer.

Revision Content

XMLSerializer can be used to convert DOM subtree or DOM document into text. XMLSerializer is available to unprivileged scripts.

 

For more information about using XMLSerializer in Firefox extensions, please see the documentation for nsIDOMSerializer.

Methods

serializeToString
Returns the serialized subtree of a string.
serializeToStream {{ non-standard_inline() }}{{ obsolete_inline("20") }}
The subtree rooted by the specified element is serialized to a byte stream using the character set specified.

Examples

 var s = new XMLSerializer();
 var d = document;
 var str = s.serializeToString(d);
 alert(str);

This example uses XMLSerializer with insertAdjacentHTML() to insert a newly created DOM Node into the Document's body. Because insertAdjacentHTML() accepts a string and not a Node for its second parameter, XMLSerializer is used to first convert the node into a string.

 

var inp = document.createElement('input');
var XMLS = new XMLSerializer(); 
var inp_xmls = XMLS.serializeToString(inp); // First convert DOM node into a string


// Insert a newly created node into the document's body
document.body.insertAdjacentHTML('afterbegin', inp_xmls);

{{ non-standard_header() }} {{ obsolete_header("20") }}

 var s = new XMLSerializer();
 var stream = {
   close : function()
   {
     alert("Stream closed");
   },
   flush : function()
   {
   },
   write : function(string, count)
   {
     alert("'" + string + "'\n bytes count: " + count + "");
   }
 };
 s.serializeToStream(document, stream, "UTF-8");

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} 9.0 {{CompatVersionUnknown}} 3.0.4
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

Revision Source

<div dir="ltr" id="result_box" style="text-align: left;">
  XMLSerializer can be used to convert DOM subtree or DOM document into text. XMLSerializer is available to unprivileged scripts.</div>
<p>&nbsp;</p>
<p>For more information about using <code>XMLSerializer</code> in Firefox extensions, please see the documentation for <a href="/en-US/docs/nsIDOMSerializer" title="nsIDOMSerializer"><code>nsIDOMSerializer</code></a>.</p>
<h2 id="Methods" name="Methods">Methods</h2>
<dl>
  <dt id="serializeToString">
    serializeToString</dt>
  <dd>
    Returns the serialized subtree of a string.</dd>
  <dt id="serializeToStream">
    serializeToStream {{ non-standard_inline() }}{{ obsolete_inline("20") }}</dt>
  <dd>
    The subtree rooted by the specified element is serialized to a byte stream using the character set specified.</dd>
</dl>
<h2 id="Examples" name="Examples">Examples</h2>
<pre class="brush: js">
 var s = new XMLSerializer();
 var d = document;
 var str = s.serializeToString(d);
 alert(str);</pre>
<p>This example uses&nbsp;XMLSerializer with&nbsp;<a href="/en-US/docs/DOM/element.insertAdjacentHTML" title="/en-US/docs/DOM/element.insertAdjacentHTML">insertAdjacentHTML()</a>&nbsp;to insert a newly created DOM Node into the Document's body. Because&nbsp;<a href="/en-US/docs/DOM/element.insertAdjacentHTML" title="/en-US/docs/DOM/element.insertAdjacentHTML">insertAdjacentHTML()</a>&nbsp;accepts a string and not a Node for its second parameter,&nbsp;XMLSerializer is used to first convert the node into a string.</p>
<p>&nbsp;</p>
<pre class="brush: js">
var inp = document.createElement('input');
var XMLS = new XMLSerializer(); 
var inp_xmls = XMLS.serializeToString(inp); // First convert DOM node into a string


// Insert a newly created node into the document's body
document.body.insertAdjacentHTML('afterbegin', inp_xmls);</pre>
<p>{{ non-standard_header() }} {{ obsolete_header("20") }}</p>
<pre class="brush: js">
 var s = new XMLSerializer();
 var stream = {
   close : function()
   {
     alert("Stream closed");
   },
   flush : function()
   {
   },
   write : function(string, count)
   {
     alert("'" + string + "'\n bytes count: " + count + "");
   }
 };
 s.serializeToStream(document, stream, "UTF-8");
</pre>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari (WebKit)</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>9.0</td>
        <td>{{CompatVersionUnknown}}</td>
        <td><span style="color: rgb(85, 85, 85); font-family: arial, sans-serif; font-size: 13px; line-height: 18px; background-color: rgb(248, 248, 248); ">3.0.4</span></td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/Parsing_and_serializing_XML" title="en-US/Parsing_and_serializing_XML">Parsing and serializing XML</a></li>
  <li><a href="/en-US/XMLHttpRequest" title="en-US/XMLHttpRequest">XMLHttpRequest</a></li>
  <li><a href="/en-US/DOMParser" title="en-US/DOMParser">DOMParser</a></li>
</ul>
Revert to this revision