[Golang] getElementById via net/html Package


Introduction

Equivalent of JavaScript getElementById via Go net/html package.

Install net/html package

$ go get -u golang.org/x/net/html

getElementById

getelementbyid.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package getelementbyid

import (
	"golang.org/x/net/html"
)

func GetAttribute(n *html.Node, key string) (string, bool) {
	for _, attr := range n.Attr {
		if attr.Key == key {
			return attr.Val, true
		}
	}
	return "", false
}

func checkId(n *html.Node, id string) bool {
	if n.Type == html.ElementNode {
		s, ok := GetAttribute(n, "id")
		if ok && s == id {
			return true
		}
	}
	return false
}

func traverse(n *html.Node, id string) *html.Node {
	if checkId(n, id) {
		return n
	}

	for c := n.FirstChild; c != nil; c = c.NextSibling {
		result := traverse(c, id)
		if result != nil {
			return result
		}
	}

	return nil
}

func getElementById(n *html.Node, id string) *html.Node {
	return traverse(n, id)
}

Usage & Test:

getelementbyid_test.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package getelementbyid

import (
	"golang.org/x/net/html"
	"strings"
	"testing"
)

const indexHtml = `<!DOCTYPE html>
<html>
<head><title>[Go] HTML table to reStructuredText list-table</title></head>
<body>
  <table>
    <tr><td id="foo">R1, C1</td><td>R1, C2</td></tr>
    <tr><td>R2, C1</td><td>R2, C2</td></tr>
  </table>
</body>
</html>`

func TestTable2Rst(t *testing.T) {
	doc, err := html.Parse(strings.NewReader(indexHtml))
	if err != nil {
		panic("Fail to parse!")
	}

	r1 := getElementById(doc, "foo")
	if r1.Data != "td" || r1.FirstChild.Data != "R1, C1" {
		t.Error("wrong element whose id is foo")
	}

	r2 := getElementById(doc, "foo2")
	if r2 != nil {
		t.Error("foo2 should not exist!")
	}
}

Run Code on Go Playground


Tested on: Ubuntu Linux 15.10, Go 1.6.1.


References:

[1]jquery iterate over elements - Google search
[2]
[3]github.com/PuerkitoBio/goquery - GoDoc
[4][Golang] Iterate over All DOM Elements in HTML