DOM: HTML DOM for XML
XML also defines an XML DOM like HTML DOM, which provides mostly the same API.
All modern browsers have a built-in XML parser that converts XML text into an in-memory DOM tree.
There are two main ways to parse.
DOMParser: Parses an XML string directly.
jsconst parser = new DOMParser(); const doc = parser.parseFromString(xmlString, "text/xml");XMLHttpRequest: Fetches XML from a URL and parses it..responseXMLgives you the DOM tree directly.
Once parsed, the XML becomes a tree of node objects (elements, attributes, text). You navigate it with standard DOM methods (.getElementsByTagName(), .querySelector(), etc.).
If the XML is malformed, DOMParser doesn't throw. Instead it returns a document containing a <parsererror> element.