Closed
Description
I am wondering how I can detect errorneous input when using DOMParse.parseFromString(). To me it seems, that a errorneous element results in just ignoring it.
Here is a simple demonstration:
errorneous.xml (missing '<' at the beginning of line 13):
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>
</catalog>
parse.js:
const fs = require("fs");
const DOMParser = require("xmldom").DOMParser;
let fileContent = fs.readFileSync("errorneous.xml", "utf-8");
let xmlDocument = new DOMParser().parseFromString(fileContent, "text/xml");
console.log(xmlDocument.getElementsByTagName("book").length); // 2
console.log(xmlDocument.getElementsByTagName("title").length); // 1
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Done