mozilla

Revision 453107 of XMLSerializer

  • Revision slug: XMLSerializer
  • Revision title: XMLSerializer
  • Revision id: 453107
  • Created:
  • Creator: teoli
  • Is current revision? Yes
  • Comment

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

XMLSerializer.serializeToString()
Returns the serialized subtree of a string.
XMLSerializer.serializeToStream() {{ non-standard_inline() }}{{ deprecated_inline() }}
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);

The next 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);

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} 9.0 {{CompatVersionUnknown}} 3.0.4
serializeToStream() {{ non-standard_inline() }} {{CompatNo}} Removed in {{CompatGeckoDesktop("20.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
serializeToStream() {{ non-standard_inline() }} {{CompatNo}} Removed in {{CompatGeckoDesktop("20.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

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">
    <code>XMLSerializer.serializeToString</code>()</dt>
  <dd>
    Returns the serialized subtree of a string.</dd>
  <dt id="serializeToStream">
    <code>XMLSerializer.serializeToStream()</code> {{ non-standard_inline() }}{{ deprecated_inline() }}</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>The next 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>
<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>
<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>3.0.4</td>
      </tr>
      <tr>
        <td><code>serializeToStream()</code> {{ non-standard_inline() }}</td>
        <td>{{CompatNo}}</td>
        <td>Removed in {{CompatGeckoDesktop("20.0")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</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>
      <tr>
        <td><code>serializeToStream()</code> {{ non-standard_inline() }}</td>
        <td>{{CompatNo}}</td>
        <td>Removed in {{CompatGeckoDesktop("20.0")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</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