DOMParser
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheDOMParser
interface providesthe ability to parseXML orHTML source code from astring into a DOMDocument
.
You can perform the opposite operation—converting a DOM tree into XML or HTMLsource—using theXMLSerializer
interface.
In the case of an HTML document, you can also replace portions of the DOM with new DOMtrees built from HTML by setting the value of theElement.innerHTML
andouterHTML
properties. These properties can also beread to fetch HTML fragments corresponding to the corresponding DOM subtree.
Note thatXMLHttpRequest
can parse XML and HTML directlyfrom a URL-addressable resource, returning aDocument
in itsresponse
property.
Note:Be aware thatblock-level elementslike<p>
will be automatically closed if anotherblock-level element is nested inside and therefore parsed before the closing</p>
tag.
Constructor
DOMParser()
Creates a new
DOMParser
object.
Instance methods
DOMParser.parseFromString()
Parses a string using either the HTML parser or the XML parser, returning an
HTMLDocument
orXMLDocument
.
Examples
The documentation forDOMParser.parseFromString()
, this interface's only method, contains examples for parsing XML, SVG, and HTML strings.
Specifications
Specification |
---|
HTML # dom-parsing-and-serialization |
Browser compatibility
See also
- Parsing and serializing XML
XMLHttpRequest
XMLSerializer
JSON.parse()
- counterpart forJSON
documents.