DOMParser

This object is available to unprivileged JavaScript. It implements the following interfaces:

Properties

nsIURI baseURI

Set/Get the baseURI, may be needed when called from native code.


Methods

Document parseFromBuffer ( arrayof PRUint8 buf , int bufLen , char* contentType ) Document parseFromStream ( nsIInputStream stream , char* charset , PRInt32 contentLength , char* contentType ) Document parseFromString ( PRUnichar* str , char* contentType )

Document parseFromBuffer ( arrayof PRUint8 buf , int bufLen , char* contentType )

The buffer is parsed into a DOM document. The charset is determined from the xml entity decl.

Arguments:
buf: The octet array data to be parsed
bufLen: Length (in bytes) of the data
contentType: The content type of the data (see parseFromStream)

Document parseFromStream ( nsIInputStream stream , char* charset , PRInt32 contentLength , char* contentType )

The byte stream passed in is parsed into a DOM document.

Not accessible from web content.

Arguments:
stream: The byte stream whose contents are parsed
charset: The character set that was used to encode the byte stream. NULL if not specified.
contentLength: The number of bytes in the input stream.
contentType: The content type of the string - either text/xml, application/xml, or application/xhtml+xml. Must not be NULL.

Document parseFromString ( PRUnichar* str , char* contentType )

The string passed in is parsed into a DOM document.

Arguments:
str: The UTF16 string to be parsed
contentType: The content type of the string (see parseFromStream)

Reference documentation is generated from Mozilla's source.

Add a note User Contributed Notes
June 4, 2004, 2:12 pm cstehlin at sejer dot fr
If the parsed XML contains error, no exception is thrown. Instead, the returned DOM document contains nodes describing the error, as follows :

#document
<parsererror>
Text describing the error
<sourcetext>
The source XML we tried to parse
</sourcetext>
</parsererror>
April 18, 2004, 10:43 am com-b at iperbole dot bologna dot it
It should be noted that the DOMParser is *not* capable of parsing content-type "text/html". It throws a NOT_IMPLEMENTED error. It is possible to parse html (docs or fragments) using "text/xml","application/xml" or "application/xhtml+xml" though. (I have no idea of what the differences are)

For parsing HTML, you can use innerHTML, or a range's createContextualFragment function - Neil

Copyright © 1999 - 2005 XULPlanet.com