web api interface
DOMParser
The DOMParser interface provides the ability to parse XML or HTML source code from a string into a DOM Document.
You can perform the opposite operation—converting a DOM tree into XML or HTML source—using the XMLSerializer interface.
In the case of an HTML document, you can also replace portions of the DOM with new DOM trees built from HTML by setting the value of the innerHTML and outerHTML properties. These properties can also be read to fetch HTML fragments corresponding to the corresponding DOM subtree.
Note that XMLHttpRequest can parse XML and HTML directly from a URL-addressable resource, returning a Document in its response property.
[!NOTE] Be aware that block-level elements like
<p>will be automatically closed if another block-level element is nested inside and therefore parsed before the closing</p>tag.
Constructor
DOMParser()- : Creates a new
DOMParserobject.
- : Creates a new
Instance methods
parseFromString()- : Parses an input
TrustedHTMLinstance or string as HTML or XML and returns aDocument.
- : Parses an input
Examples
The documentation for parseFromString(), this interface’s only method, contains examples for parsing XML, SVG, and HTML strings.
Specifications
Browser compatibility
See also
- Parsing and serializing XML
XMLHttpRequestXMLSerializerparse()- counterpart forJSONdocuments.