Can you operate on XML / HTML documents in node.js?
Is it possible to load an XML / HTML document with node.js and perform operations such as getE开发者_运维技巧lementsByTagName ?
node.js doesn't give you a DOM "out of the box", but there is a great module that provides a proper HTML DOM: https://github.com/tmpvar/jsdom.
Edit: there are also several modules that help with interacting with XML. Here's a list of them on the wiki (this page is deprecated): https://github.com/joyent/node/wiki/modules#wiki-parsers-xml
Cheerio: Tiny, fast, and elegant implementation of core jQuery designed specifically for the server.
var cheerio = require('cheerio'),
$ = cheerio.load('<h2 class="title">Hello world</h2>');
$('h2.title').text('Hello there!');
$('h2').addClass('welcome');
$.html();
//=> <h2 class="title welcome">Hello there!</h2>
Also check out https://github.com/robtweed/ewdDOM
This is a persistent DOM implementation using the Globals database
精彩评论