Socket
Book a DemoInstallSign in
Socket

github.com/mattn/go-libxml2

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/mattn/go-libxml2

Source
Go Modules
Version
v0.0.0-20150603035051-0d71f0fcd0db
Version published
Created
Source

go-libxml2

Build Status

GoDoc

Interface to libxml2, with DOM interface

This library is still in very early stages of development. API may still change

import (
	"log"
	"net/http"

	"github.com/lestrrat/go-libxml2"
)

func ExampleHTML() {
	res, err := http.Get("http://golang.org")
	if err != nil {
		panic("failed to get golang.org: " + err.Error())
	}

	doc, err := libxml2.ParseHTML(res.Body)
	if err != nil {
		panic("failed to parse HTML: " + err.Error())
	}
	defer doc.Free()

	doc.Walk(func(n Node) error {
		log.Printf(n.NodeName())
		return nil
	})

	nodes, err := doc.FindNodes(`//div[@id="menu"]/a`)
	if err != nil {
		panic("failed to evaluate xpath: " + err.Error())
	}

	for i := 0; i < len(nodes); i++ {
		log.Printf("Found node: %s", nodes[i].NodeName())
	}
}

Caveats

Other libraries

There exists many similar libraries. I want speed, I want DOM, and I want XPath.When all of these are met, I'd be happy to switch to another library.

For now my closest contender was xmlpath, but as of this writing it suffers in the speed (for xpath) area a bit:

go test -tags bench -bench=. -benchtime=5s
PASS
BenchmarkXmlpath      100000         88764 ns/op
BenchmarkLibxml2      300000         22509 ns/op
ok      github.com/lestrrat/go-libxml2  24.926s

FAQs

Package last updated on 03 Jun 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts