Home

DOMParser

DOMParser is a web platform API that provides a way to parse a string of markup into a DOM Document. It is part of the browser’s standard DOM APIs and can be accessed by creating a new instance via new DOMParser().

The primary method is parseFromString, which takes two arguments: the markup string and a MIME type indicating

The result of parseFromString is a separate Document object. It does not execute scripts or load external

Usage examples are common for extracting data from strings or cleaning up fragments for templating. For example:

DOMParser is widely supported in modern browsers and standardized as part of the web platform, enabling safe,

how
to
parse
the
string.
Common
MIME
types
include
"text/html"
for
HTML
parsing
and
"text/xml"
or
"application/xml"
for
XML
parsing.
Some
browsers
also
support
"image/svg+xml"
for
SVG
content.
The
behavior
varies
with
the
type:
"text/html"
uses
the
HTML
parser,
while
XML
types
use
the
XML
parser.
resources.
For
XML
documents,
you
can
inspect
the
document
element
and
traverse
the
resulting
tree.
If
parsing
fails,
some
browsers
insert
a
parsererror
element
in
the
returned
document.
The
document
is
not
automatically
integrated
into
the
current
page,
though
nodes
can
be
imported
if
needed
with
document.importNode.
var
parser
=
new
DOMParser();
var
doc
=
parser.parseFromString("<root><child/></root>",
"text/xml");
You
can
then
access
doc.documentElement
and
navigate
its
structure.
script-free
parsing
of
markup
strings.