EN
Node.js - parse html to DOM with htmlparser2 library
7
points
In this short article, we would like to show how in Node.js parse HTML to Simple DOM - it is not real DOM but is enough in many cases giving good parser performance (check performance section).
Note: the main advantage of this library is portability (Works in any JavaScript - even with React and SSR)
Library installation
npm install --save htmlparser2
Usage example
index.js
file:
const htmlparser2 = require("htmlparser2");
const handler = new htmlparser2.DomHandler();
const parser = new htmlparser2.Parser(handler);
parser.write('<div><p>1. Some text .../p><p>2. Some text .../p></div>');
parser.end();
const root = handler.root;
console.log(root);
Screenshot:
TypeScript version
index.ts
file:
import { DomHandler, Parser } from 'htmlparser2';
const handler = new DomHandler();
const parser = new Parser(handler);
parser.write('<div><p>1. Some text .../p><p>2. Some text .../p></div>');
parser.end();
const root = handler.root;
console.log(root);