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

1

7

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?

Hammond answered 20/5, 2019 at 0:36 Comment(0)
H
21

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.

Hammond answered 20/5, 2019 at 0:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.