我正在将 html 从 api 转换为 dom 元素,以便我可以解析 xml 内容。内容示例:
\n\nvar 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.\xc2\xa0He was 77 years old.</p>\';\n\nvar convertHtmlToDom = function(html) {\n var parser = new DOMParser(); \n return parser.parseFromString(html, "text/xml");\n}\nRun Code Online (Sandbox Code Playgroud)\n\n除了我收到此错误:
\n\n<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\n</div><h3>Below is a rendering of the page up to the first error.</h3></parsererror>\nRun Code Online (Sandbox Code Playgroud)\n\n我怎样才能抑制或忽略这些错误?
\n正如我在评论中提到的,如果输入是 HTML,请不要尝试将其解析为 XML。XML 解析器必须对其输入更加严格,例如不匹配的标签或多个根节点。
因此,以下内容应该有效:
parser.parseFromString(html, 'text/html');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2317 次 |
| 最近记录: |