/
xmlpath.go
42 lines (32 loc) · 853 Bytes
/
xmlpath.go
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
package pathfinder
import (
bytes2 "bytes"
"fmt"
"github.com/antchfx/xmlquery"
)
// AntchfxXMLFinder represents implementation of XPath from https://github.com/antchfx/xmlquery
type AntchfxXMLFinder struct{}
func NewAntchfxXMLFinder() AntchfxXMLFinder {
return AntchfxXMLFinder{}
}
func (a AntchfxXMLFinder) Find(expr string, bytes []byte) (any, error) {
parser, err := xmlquery.Parse(bytes2.NewReader(bytes))
if err != nil {
return nil, err
}
nodes, err := xmlquery.QueryAll(parser, expr)
if err != nil {
return nil, err
}
if len(nodes) == 1 {
return any(nodes[0].InnerText()), nil
}
if len(nodes) > 1 {
results := make([]any, 0, len(nodes))
for _, node := range nodes {
results = append(results, node.InnerText())
}
return results, nil
}
return nil, fmt.Errorf("could not find %s in given XML bytes", expr)
}