Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript DomParser忽略/suppress解析错误_Javascript_Dom - Fatal编程技术网

Javascript DomParser忽略/suppress解析错误

Javascript DomParser忽略/suppress解析错误,javascript,dom,Javascript,Dom,我正在将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 an

我正在将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");
}
极具影响力的导演、恐怖电影和独立电影巨头于7月16日去世,享年77岁; var convertHTMLTOM=函数(html){ var parser=新的DOMParser(); 返回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>
此页面包含以下错误:第1列第2行的错误:文档末尾的额外内容
下面是第一个错误之前的页面呈现。

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

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

因此,应采取以下措施:

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

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

因此,应采取以下措施:

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

不要将其解析为XML。试试parser.parseFromString(html,'text/html')。不要将其解析为XML。试试parser.parseFromString(html,'text/html')