Skip to content

htmlqueryvssax-js

MIT 8 1 710
58.1 thousand (month) Feb 07 2019 v1.3.2(21 days ago)
1,082 1 97 ISC
Feb 09 2011 143.7 million (month) 1.4.1(a month ago)

htmlquery is a Go library that allows you to parse and extract data from HTML documents using XPath expressions. It provides a simple and intuitive API for traversing and querying the HTML tree structure, and it is built on top of the popular Goquery library.

sax-js is a streaming XML parser for Node.js that is built on top of the sax C library. It is designed to be fast, low-memory, and easy to use. It is commonly used for parsing large XML files, as it allows you to process the XML data incrementally, rather than loading the entire file into memory at once.

sax-js is a low-level html tree parser and does not provide html query capabilities (like CSS selectors) though it can be useful in HTML tree parsing and serialization.

Example Use


package main

import (
  "fmt"
  "log"

  "github.com/antchfx/htmlquery"
)

func main() {
  // Parse the HTML string
  doc, err := htmlquery.Parse([]byte(`
    <html>
      <body>
        <h1>Hello, World!</h1>
        <ul>
          <li>Item 1</li>
          <li>Item 2</li>
          <li>Item 3</li>
        </ul>
      </body>
    </html>
  `))
  if err != nil {
    log.Fatal(err)
  }

  // Extract the text of the first <h1> element
  h1 := htmlquery.FindOne(doc, "//h1")
  fmt.Println(htmlquery.InnerText(h1)) // "Hello, World!"

  // Extract the text of all <li> elements
  lis := htmlquery.Find(doc, "//li")
  for _, li := range lis {
    fmt.Println(htmlquery.InnerText(li))
  }
  // "Item 1"
  // "Item 2"
  // "Item 3"
}
const fs = require("fs");
const sax = require("sax");

const xmlStream = fs.createReadStream("example.xml");
const saxParser = sax.createStream(true, {});

saxParser.on("opentag", function(node) {
    console.log(`<${node.name}>`);
});

saxParser.on("closetag", function(nodeName) {
    console.log(`</${nodeName}>`);
});

saxParser.on("text", function(text) {
    console.log(text);
});

xmlStream.pipe(saxParser);

Alternatives / Similar


Was this page helpful?