最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Save XML parsed by JSDOM as a file - Stack Overflow

programmeradmin2浏览0评论

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="" xmlns:xsd="">

Note that Globe is the root element of the source XML. It also produces everything in lower-case.

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/2001/XMLSchema-instance" xmlns:xsd="http://www.w3/2001/XMLSchema">

Note that Globe is the root element of the source XML. It also produces everything in lower-case.

Share Improve this question asked Jan 9, 2020 at 17:07 Tomáš ZatoTomáš Zato 53.2k63 gold badges308 silver badges822 bronze badges 2
  • You might need to tell JSDOM that it's not parsing HTML. – evolutionxbox Commented Jan 9, 2020 at 17:13
  • Yeah, it looks like it interpreted the document as HTML. It wasn't immediately apparent, since the selectors and such worked. – Tomáš Zato Commented Jan 9, 2020 at 17:15
Add a ment  | 

2 Answers 2

Reset to default 13

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;

An alternative (and easy) way to render xml is passing { contentType: "text/xml" } property to JSDOM initializer.

With Content Type:

let testString = `<root><child1/><child2><grandchild>name1</grandchild></child2></root>`

const { JSDOM } = require("jsdom");
const dom = new JSDOM(testString, { contentType: "text/xml"});

const xmlString = dom.window.document.querySelector("root").outerHTML;

console.log(testString);
console.log(xmlString);

outputs:

<root><child1/><child2><grandchild>name1</grandchild></child2></root>
<root><child1/><child2><grandchild>name1</grandchild></child2></root>

But without content type (defaults to text/html):

let testString = `<root><child1/><child2><grandchild>name1</grandchild></child2></root>`

const { JSDOM } = require("jsdom");
const dom = new JSDOM(testString);

const xmlString = dom.window.document.querySelector("root").outerHTML;

console.log(testString);
console.log(xmlString);

renders as:

<root><child1/><child2><grandchild>name1</grandchild></child2></root>
<root><child1><child2><grandchild>name1</grandchild></child2></child1></root>
发布评论

评论列表(0)

  1. 暂无评论