Languages
[Edit]
EN

Node.js - parse html to DOM with htmlparser2 library

7 points
Created by:
Root-ssh
175400

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:

Node.js HTML parsing with htmlparser2 library.
Node.js HTML parsing with htmlparser2 library.

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);

See also

  1. JavaScript - parse HTML

  2. JavaScript - parse XML

References

  1.  https://www.npmjs.com/package/htmlparser2
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join