DOMParser

  • Revision slug: DOM/DOMParser
  • Revision title: DOMParser
  • Revision id: 37380
  • Created:
  • Creator: Bzbarsky
  • Is current revision? No
  • Comment /* DOMParser changes in Firefox 3 */

Revision Content

DOMParser can be used to parse strings and streams of XML text. It can't be used to parse HTML "tag soup". DOMParser is available to unprivileged scripts.

{{template.Note("<code>DOMParser</code> is mainly useful for applications and extensions based on Mozilla platform. While it\'s available to web pages, it\'s not part of any standard and level of support in other browsers is unknown.")}}

DOMParser changes in Firefox 3

{{template.Note("This section covers changes introduced to <code>DOMParser</code> in Gecko 1.9.")}}

  • When a DOMParser is instantiated by calling new DOMParser(), it inherits the calling code's principal and the documentURI and baseURI of the window the constructor came from.
  • If the caller has UniversalXPConnect privileges, it can pass parameters to new DOMParser(). If fewer than three parameters are passed, the remaining parameters will default to null.
    • The first parameter is the principal to use; this overrides the default principal normally inherited.
    • The second parameter is the documentURI to use.
    • The third parameter is the baseURI to use.
  • If you instantiate a DOMParser by calling createInstance(), and you don't call the DOMParser's init() method, attempting to initiate a parsing operation will automatically call init() on the DOMParser with a null principal and null pointers for documentURI and baseURI.

Error handling

Note that if the parsing process failed, DOMParser currently does not throw an exception, but instead returns an error document (see {{template.Bug(45566)}}):

<parsererror xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml">
(error description)
<sourcetext>(a snipper of the source XML)</sourcetext>
</parsererror>

Example

var parser = new DOMParser();
var doc = parser.parseFromString(aStr, "text/xml");

See also

{{ wiki.languages( { "fr": "fr/DOMParser", "ja": "ja/DOMParser" } ) }}

Revision Source

<p>
</p><p><code>DOMParser</code> can be used to parse strings and streams of XML text. It can't be used to parse HTML "tag soup". <code>DOMParser</code> is available to unprivileged scripts.
</p><p>{{template.Note("&lt;code&gt;DOMParser&lt;/code&gt; is mainly useful for applications and extensions based on Mozilla platform. While it\'s available to web pages, it\'s not part of any standard and level of support in other browsers is unknown.")}}
</p>
<h3 name="DOMParser_changes_in_Firefox_3">DOMParser changes in Firefox 3</h3>
<p>{{template.Note("This section covers changes introduced to &lt;code&gt;DOMParser&lt;/code&gt; in Gecko 1.9.")}}
</p>
<ul><li> When a <code>DOMParser</code> is instantiated by calling <code>new DOMParser()</code>, it inherits the calling code's principal and the <code>documentURI</code> and <code>baseURI</code> of the window the constructor came from.
</li><li> If the caller has UniversalXPConnect privileges, it can pass parameters to <code>new DOMParser()</code>.  If fewer than three parameters are passed, the remaining parameters will default to <code>null</code>.
<ul><li> The first parameter is the principal to use; this overrides the default principal normally inherited.
</li><li> The second parameter is the <code>documentURI</code> to use.
</li><li> The third parameter is the <code>baseURI</code> to use.
</li></ul>
</li><li> If you instantiate a <code>DOMParser</code> by calling <code>createInstance()</code>, and you don't call the <code>DOMParser</code>'s <code>init()</code> method, attempting to initiate a parsing operation will automatically call <code>init()</code> on the <code>DOMParser</code> with a null principal and <code>null</code> pointers for <code>documentURI</code> and <code>baseURI</code>.
</li></ul>
<h3 name="Error_handling"> Error handling </h3>
<p>Note that if the parsing process failed, <code>DOMParser</code> currently does not throw  an exception, but instead returns an error document (see {{template.Bug(45566)}}):
</p>
<pre class="eval">&lt;parsererror xmlns="<span class="plain">http://www.mozilla.org/newlayout/xml/parsererror.xml</span>"&gt;
<i>(error description)</i>
&lt;sourcetext&gt;<i>(a snipper of the source XML)</i>&lt;/sourcetext&gt;
&lt;/parsererror&gt;
</pre>
<h3 name="Example"> Example </h3>
<pre class="eval">var parser = new DOMParser();
var doc = parser.parseFromString(aStr, "text/xml");
</pre>
<h3 name="See_also"> See also </h3>
<ul><li> <a class="external" href="http://www.xulplanet.com/references/objref/DOMParser.html">Reference</a> (XULPlanet)
</li><li> <a href="en/Parsing_and_serializing_XML">Parsing and serializing XML</a>
</li><li> <a class="external" href="http://xulplanet.com/tutorials/mozsdk/xmlparse.php">Parsing and serializing XML</a> (XULPlanet)
</li><li> <a class="external" href="http://kb.mozillazine.org/Parsing_and_serializing_XML#Parsing_strings_into_DOM_trees">Parsing and serializing XML</a> (MozillaZine)
</li><li> <a href="en/XMLHttpRequest">XMLHttpRequest</a>
</li><li> <a href="en/XMLSerializer">XMLSerializer</a>
</li></ul>
<div class="noinclude">
</div>
{{ wiki.languages( { "fr": "fr/DOMParser", "ja": "ja/DOMParser" } ) }}
Revert to this revision