I can parse XML like this:
import JSDOM from "jsdom";
const myXml = "... some xml loaded from somewhere ...";
const dom = new JSDOM.JSDOM(myXml);
I was really surprised when I googled "jsdom save xml" and I got nothing. I thought JSDOm is one of the most popular libraries for XML manipulation.
In one answer, I've seen this:
window.document.documentElement.outerHTML
That produces garbage code for some reason, for example it converts:
<Node>
<Child attr="attr"/>
<Child attr="attr"/>
<Child attr="attr"/>
<Child attr="attr"/>
</Node>
to
<Node>
<Child attr="attr">
<Child attr="attr">
<Child attr="attr">
<Child attr="attr">
</Child></Child></Child></Child>
</Node>
It also starts the document with:
<html><head></head><body><globe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
Note that Globe
is the root element of the source XML. It also produces everything in lower-case.
The method of saving the document was correct. But the method of parsing is wrong. JSDOM
requires HTML and creates an instance of browser DOM. And it must be used as such, so the steps are the same as when parsing XML in browser:
// Create empty DOM, the imput param here is for HTML not XML, and we don want to parse HTML
const dom = new JSDOM.JSDOM("");
// Get DOMParser, same API as in browser
const DOMParser = dom.window.DOMParser;
const parser = new DOMParser;
// Create document by parsing XML
const document = parser.parseFromString(xml, "text/xml");
// save the xml after modifications
const xmlString = document.documentElement.outerHTML;