It seems that all major browsers implement the DOMParser API so that XML can be parsed into a DOM and then queried using XPath, getElementsByTagName, etc...
However, detecting parsing errors seems to be trickier. DOMParser.prototype.parseFromString
always returns a valid DOM. When a parsing error occurs, the returned DOM contains a <parsererror>
element, but it's slightly different in each major browser.
Sample JavaScript:
xmlText = '<root xmlns="http://default" xmlns:other="http://other"><child><otherr:grandchild/></child></root>';
parser = new DOMParser();
dom = parser.parseFromString(xmlText, 'application/xml');
console.log((new XMLSerializer()).serializeToString(dom));
Result in Opera:
DOM's root is a <parsererror>
element.
<?xml version="1.0"?><parsererror xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml">Error<sourcetext>Unknown source</sourcetext></parsererror>
Result in Firefox:
DOM's root is a <parsererror>
element.
<?xml-stylesheet href="chrome://global/locale/intl.css" type="text/css"?>
<parsererror xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml">XML Parsing Error: prefix not bound to a namespace
Location: http://fiddle.jshell.net/_display/
Line Number 1, Column 64:<sourcetext><root xmlns="http://default" xmlns:other="http://other"><child><otherr:grandchild/></child></root>
---------------------------------------------------------------^</sourcetext></parsererror>
Result in Safari:
The <root>
element parses correctly but contains a nested <parsererror>
in a different namespace than Opera and Firefox's <parsererror>
element.
<root xmlns="http://default" xmlns:other="http://other"><parsererror xmlns="http://www.w3.org/1999/xhtml" style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>This page contains the following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 1 at column 50: Namespace prefix otherr on grandchild is not defined
</div><h3>Below is a rendering of the page up to the first error.</h3></parsererror><child><otherr:grandchild/></child></root>
Am I missing a simple, cross-browser way of detecting if a parsing error occurred anywhere in the XML document? Or must I query the DOM for each of the possible <parsererror>
elements that different browsers might generate?
.getElementsByTagName("parseerror")
on the root DOM node and assume that there was an error if the length of the returned node list is greater than zero? – Pointy<parsererror>
elements but still be totally valid XML (elements would be from a different namespace) So I'd have to make multiple calls to.getElementsByTagNameNS(namespace, 'parsererror')
for thenamespace
URIs from each browser. – cspotcode