-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
47 lines (39 loc) · 883 Bytes
/
helpers.go
File metadata and controls
47 lines (39 loc) · 883 Bytes
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
44
45
46
47
package httputils
import (
"encoding/json"
"errors"
"log"
"strings"
"gopkg.in/xmlpath.v2"
)
func mapToJSON(m map[string]string) string {
str, err := json.Marshal(m)
if err != nil {
log.Println(err)
return ""
}
return string(str)
}
func JSONToMap(s string) map[string]string {
var m map[string]string
err := json.Unmarshal([]byte(s), &m)
if err != nil {
log.Println(err)
}
return m
}
func FindXPath(html string, xpath string) (string, error) {
reader := strings.NewReader(html)
xmlroot, err := xmlpath.ParseHTML(reader)
if err != nil {
log.Println(err)
}
// correios table with the package information
// example `//table[contains(@class, 'listEvent')]`
path := xmlpath.MustCompile(xpath)
if value, ok := path.String(xmlroot); ok {
log.Println("Found:", value)
return value, nil
}
return "", errors.New("No matches with xpath: " + xpath)
}