Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to silent all the warning messages of XML DOM in Node.js

I was using a node module xmldom. However, it always prints out a huge chunk of warnings and errors like the following:

@#[line:484,col:1]
[xmldom warning]        attribute "hidden" missed quot(")!!
@#[line:517,col:1]
[xmldom warning]        unclosed xml attribute
@#[line:517,col:1]
[xmldom warning]        unclosed xml attribute
@#[line:518,col:1]
[xmldom warning]        attribute "center" missed quot(")!!
@#[line:522,col:1]
[xmldom warning]        attribute "6" missed quot(")!!

I was wondering how to explicitly silent all those warnings and errors without touching node or package.json themselves?

like image 545
Jeff Hu Avatar asked May 20 '19 00:05

Jeff Hu


1 Answers

I luckily found the answer with a reference to this question. The workaround for me is to replace the original dom instantiation:

var doc = new dom().parseFromString(body);

with the following options:

var doc = new dom({
    locator: {},
    errorHandler: { warning: function (w) { }, 
    error: function (e) { }, 
    fatalError: function (e) { console.error(e) } }
}).parseFromString(body);

We must understand that hiding the warnings and errors could not solve the problem. Therefore, I suggest using this techniques only when the correctness of the input have no impact on the later logics, or the warning messages are overwhelming the other console messages.

Hope it helps the communities.

like image 92
Jeff Hu Avatar answered Sep 24 '22 00:09

Jeff Hu