func main() { // Find ourself some html resp, err := http.Get("https://github.com/Wessie/unhtml/commits/master") if err != nil { log.Fatal(err) } // Parse the HTML, accepts an io.Reader d, err := unhtml.NewDecoder(resp.Body) if err != nil { log.Fatal(err) } // Unmarshal into a struct with xpath tags for extracting things result := GithubCommits{} if err := d.Unmarshal(&result); err != nil { log.Fatal(err) } // Convert to JSON for pretty printing the result b, err := json.MarshalIndent(result, "", " ") if err != nil { log.Fatal(err) } fmt.Println(string(b)) }
func Parse(r io.Reader) ([]Item, error) { for i, v := range xml.HTMLAutoClose { if v == "link" { xml.HTMLAutoClose[i] = "bogus" break } } d, err := unhtml.NewDecoder(r) if err != nil { return nil, err } var result []Item var startPath = "rss/channel/item" if err := d.UnmarshalRelative(startPath, &result); err != nil { return nil, err } return result, nil }