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 Element.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
DOMParser
object.
Methods
DOMParser.parseFromString()
-
Parses a string using either the HTML parser or the XML parser, returning an
HTMLDocument
orXMLDocument
.
Examples
The documentation for DOMParser.parseFromString()
, this interface's only method, contains examples for parsing XML, SVG, and HTML strings.
Specifications
Specification |
---|
HTML Standard (HTML) # dom-parsing-and-serialization |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
DOMParser |
1 |
12 |
1 |
9 |
8 |
1.3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
DOMParser |
1 |
12 |
1 |
9 |
8 |
1.3 |
1 |
18 |
4 |
Yes |
1 |
1.0 |
parseFromString |
1 |
12 |
1 |
9 |
8 |
1.3 |
1 |
Yes |
4 |
Yes |
1 |
Yes |
See also
- Parsing and serializing XML
XMLHttpRequest
XMLSerializer
-
JSON.parse()
- counterpart forJSON
documents.
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/DOMParser