jindw / xmldom

A PURE JS W3C Standard based(XML DOM Level2 CORE) DOMParser and XMLSerializer.
Other
819 stars 265 forks source link

It can't show error when one node does not have close anlge brackets. #198

Open jyywhw opened 7 years ago

jyywhw commented 7 years ago

I use an xml string like "xxx</b" to test xmldom, which can't show error message for it, but just pass this xml parsed.

How do I use xmldom to make it show error message?

brandonkirsch commented 7 years ago

Try assigning errorHandlers when you initialize the DOMParser.

I use a generic wrapper function that looks like this:

 function xmldomWithParseError(xmlString){

// throw exception for any parsing errors or warnings
var errorHandler = function(errorString){
    throw new Error(errorString);
}

// let's throw Exceptions when W3C/node-xmldom XMLParser.parseFromString() encounters any warnings or errors
var domOptions = {
    errorHandler: {
        warning: errorHandler,
        error: errorHandler,
        fatalError: errorHandler
    },

    locator: {}
};

var xmldom = new XmlDom.DOMParser(domOptions).parseFromString(xmlString, 'text/xml');

return xmldom;
}