An XML file can be parsed into a DOM object by using JavaScript.

The parserFromString() method of the DOMparser() object can be used to parse XML document. This method takes a string or a document to be parsed and content type as arguments and produces a result in form of a DOM object. The content type is always a “text/xml”. The string to be parsed has to be in a proper XML format. If there is an error in parsing, each browser handles it in its own way. To make sure that the codewill not break the parser the best idea is to keep it within a try-catch statements.

The example below is showing a cross browser functional XML string parsed to DOM elements. If there is an error in parsing an alert message gets displayed, else the length of elements in a tag name will be displayed.

Example with the DOM parsing type and try-catch mode


›› go to examples ››