I am converting html from an api to dom elements so that I can parse the xml content. An example of the content:
var html = '<p><a href=".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");
}
Except I get this error:
<parsererror xmlns="" 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>
How can I suppress or ignore these errors?
I am converting html from an api to dom elements so that I can parse the xml content. An example of the content:
var html = '<p><a href="https://www.nytimes./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");
}
Except I get this error:
<parsererror xmlns="http://www.w3/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>
How can I suppress or ignore these errors?
Share Improve this question edited May 14, 2020 at 16:24 Devin Dixon asked Aug 11, 2017 at 15:55 Devin DixonDevin Dixon 12.5k24 gold badges97 silver badges179 bronze badges 1-
1
Don't parse it as XML. Try
parser.parseFromString(html, 'text/html');
. – Phylogenesis Commented Aug 11, 2017 at 15:59
1 Answer
Reset to default 8As I mentioned in my ment, if the input is HTML, do not attempt to parse it as XML. An XML parser has to be much more strict with its input, such as with unmatched tags or multiple root nodes.
As such, the following should work:
parser.parseFromString(html, 'text/html');