I use https://github.com/jindw/xmldom and want check parseerrors on XML files.
The documentation write it's necessary to overwrite locator + errorHandler on constructor of DOMParser.
But I can't find anywhere code example, how to use these in node context.
Documentation say:
//errorHandler is supported
new DOMParser({
/**
* locator is always need for error position info
*/
locator:{},
/**
* you can override the errorHandler for xml parser
* @link http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html
*/
errorHandler:{warning:function(w){console.warn(w)},error:callback,fatalError:callback}
//only callback model
//errorHandler:function(level,msg){console.log(level,msg)}
})
This is the runable code:
var DOMParser = require('xmldom').DOMParser;
let mylocator = {};
let parseLog = {errorLevel: 0};
let parser = new DOMParser({
locator: mylocator,
errorHandler: {
warning: (msg) => {manageXmlParseError(msg,1,parseLog)},
error: (msg) => {manageXmlParseError(msg,2,parseLog)},
fatalError: (msg) => {manageXmlParseError(msg,3,parseLog)},
},
});
function manageXmlParseError(msg,errorLevel,errorLog){
if( (errorLog.errorLevel == null) || (errorLog.errorLevel < errorLevel)){
errorLog.errorLevel = errorLevel;
}
if(errorLog[errorLevel.toString()] == null){
errorLog[errorLevel.toString()] = [];
}
errorLog[errorLevel.toString()].push(msg);
}
var doc = parser.parseFromString(
'<xml xmlns="a" xmlns:c="./lite">\n'+
'\t<child>test</child>\n'+
'\t<child22><<</child>\n'+
'\t<child/>\n'+
'</xml>'
,'text/xml');
console.info("parsestatus ==> " + parseLog.errorLevel + "\nlocator:" + mylocator.columnNumber + "/" + mylocator.lineNumber );