如何在 Node.js 中静默 XML DOM 的所有警告消息

Jef*_* Hu 7 xml dom xmldom node.js npm

我正在使用节点模块xmldom。但是,它总是会打印出大量警告和错误,如下所示:

@#[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(")!!
Run Code Online (Sandbox Code Playgroud)

我不知道如何来明确地沉默所有那些警告和错误而不触及节点或自己的package.json?

Jef*_* Hu 13

我幸运地找到了参考这个问题的答案。我的解决方法是替换原始dom实例化:

var doc = new dom().parseFromString(body);
Run Code Online (Sandbox Code Playgroud)

具有以下内容options

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

我们必须明白,隐藏警告和错误并不能解决问题。因此,我建议仅在输入的正确性对后面的逻辑没有影响,或者警告消息压倒其他控制台消息时才使用此技术。

希望对社区有所帮助。