Closed amitguptagwl closed 7 years ago
If you read Mozilla's DOMParser page, you will see that the .parseFromString() method does not throw errors or exceptions.
The (silly!) W3C spec says to return a Document anyways, leaving you to figure out whether it parsed or not.
node-xmldom appears to have some optional errorHandler parameters that you can pass, but I haven't had any luck getting them to fire for malformed XML strings.
I'll post an update when I find the best way to trap parsing errors with xmldom.
In order to capture errors, you must instantiate the DOMParser() with a set of error handlers.
The following bit of code will capture any warnings, errors or fatalErrors that occur during XML Parsing and will throw an exception:
// create an errorHandler callback that throws an exception var errorHandler = function(errorString){ throw new Error(errorString); }
// bind our errorHandler to warnings, errors & fatalErrors. var domOptions = { errorHandler: { warning: errorHandler, error: errorHandler, fatalError: errorHandler }, locator: {} };
// parseFromString will now call errorHandler in case of any warning or error var x = new require('xmldom').domParser(domOptions).parseFromString(xmlStr, 'text/xml');
Thanks
@brandonkirsch Thanks for confirming, this seemed like the answer, but the readme/docs don't really specify this, thanks!!
I passed an invalid XML where opening and closing tags were not same. But it dint throw any exception while it parsed it. which is not expected.