gpt4 book ai didi

Javascript DomParser 忽略/抑制解析错误

转载 作者:行者123 更新时间:2023-11-30 11:32:50 26 4
gpt4 key购买 nike

我正在将 html 从 api 转换为 dom 元素,以便我可以解析 xml 内容。内容示例:

var html = '<p><a href="https://www.nytimes.com/2017/07/16/movies/george-romero-dead.html?_r=0" target="_blank">George Romero</a>, the highly influential auteur filmmaker and giant of horror and independent cinema passed away on July 16. He was 77 years old.</p>';

var convertHtmlToDom = function(html) {
var parser = new DOMParser();
return parser.parseFromString(html, "text/xml");
}

除了我得到这个错误:

<parsererror xmlns="http://www.w3.org/1999/xhtml" style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>This page contains the following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 2 at column 1: Extra content at the end of the document
</div><h3>Below is a rendering of the page up to the first error.</h3></parsererror>

如何抑制或忽略这些错误?

最佳答案

正如我在评论中提到的,如果输入是 HTML,请不要尝试将其解析为 XML。 XML 解析器必须对其输入更加严格,例如不匹配的标签或多个根节点。

因此,以下应该有效:

parser.parseFromString(html, 'text/html');

关于Javascript DomParser 忽略/抑制解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45639260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com