尝试将DOMParser与Node JS一起使用 [英] Trying to use the DOMParser with node js
问题描述
尝试在我的js代码中使用DOMParser时遇到问题.在我的代码中,我通过xmlhttp.responseText soap响应检索了一个xml文件.我希望能够以JSON格式访问其元素,因此我的代码如下:
I am running into issues when trying to use the DOMParser in my js code. In my code, I retrieve an xml file via xmlhttp.responseText soap response. I want to be able to access its elements in JSON format, so my code looks like:
var xml = new DOMParser();
xml = xml.parseFromString(xmlhttp.responseText, 'text/xml');
var result = xmlToJson(xml);
我收到此错误消息: ReferenceError:未定义DOMParser
I get this error message: ReferenceError: DOMParser is not defined
该链接对我不起作用,因为我的JavaScript不在HTML页面中,因为它是一个node.js文件. JavaScript DOMParser访问innerHTML和其他属性
This link hasn't worked for me because my javascript isn't in the HTML page, as it is a node.js file. JavaScript DOMParser access innerHTML and other properties
推荐答案
NodeJS本身不提供许多浏览器功能,例如DOM操作或XHR,因为这不是访问DOM的典型服务器任务-您将必须使用外部库来做到这一点.
A lot of browser functionalities, like DOM manipulations or XHR, are not available natively NodeJS because that is not a typical server task to access the DOM - you'll have to use an external library to do that.
DOM的功能在很大程度上取决于库,这是您可以使用的主要工具的快速比较:
DOM capacities depends a lot on the library, here's a quick comparisons of the main tools you can use:
-
jsdom
:实现DOM级别4这是最新的DOM标准,因此您可以在现代浏览器上执行的所有操作可以在jsdom
中完成. Mocha,Vue Test Utils,Webpack Prerender SPA插件和许多其他应用程序使用的事实上的行业标准,用于在Node上进行浏览器操作.
jsdom
: implements DOM level 4 which is the latest DOM standard, so everything that you can do on a modern browser, you can do it injsdom
. It is the de-facto industry standard for doing browser stuff on Node, used by Mocha, Vue Test Utils, Webpack Prerender SPA Plugin, and many other:
const jsdom = require("jsdom");
const dom = new jsdom.JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
dom.window.document.querySelector("p").textContent; // 'Hello world'
htmlparser2
:相同,但带有以更复杂的API为代价,增强了性能和灵活性:
htmlparser2
: same, but with enhanced performances and flexibility at the price of a more complex API:
const htmlparser = require("htmlparser2");
const parser = new htmlparser.Parser({
onopentag: (name, attrib) => {
if (name=='p') console.log('a paragraph element is opening');
}
}, {decodeEntities: true});
parser.write(`<!DOCTYPE html><p>Hello world</p>`);
parser.end();
// console output: 'a paragraph element is opening'
cheerio
:基于jQuery的实现通过htmlparser2
解析HTML DOM:
cheerio
: implementation of jQuery based on HTML DOM parsing by htmlparser2
:
const cheerio = require('cheerio');
const $ = cheerio.load(`<!DOCTYPE html><p>Hello world</p>`);
$('p').text('Bye moon');
$.html(); // '<!DOCTYPE html><p>Bye moon</p>'
xmldom
:完全实现了DOM级别2并部分实现DOM级别3.既可以使用HTML,也可以使用XML
xmldom
: fully implements the DOM level 2 and partially implements the DOM level 3. Works with HTML, and with XML also
dom-parser
:正则表达式-基于DOM的解析器,它实现了一些getElementById
之类的DOM方法.由于使用正则表达式解析HTML是一个非常糟糕的主意,我不会推荐这个用于生产.
dom-parser
: regex-based DOM parser that implements a few DOM methods like getElementById
. Since parsing HTML with regular expressions is a very bad idea I wouldn't recommend this one for production.
这篇关于尝试将DOMParser与Node JS一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!