-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
executable file
·54 lines (39 loc) · 995 Bytes
/
common.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
43
44
45
46
47
48
49
50
51
52
53
54
package goxml
/*
// #cgo pkg-config: libxml-2.0
// #include <libxml/tree.h>
import "C"
import "unsafe"
import "fmt"
import "os"
*/
/*
High Level Interface functions
*/
func HtmlExtractXpathNodes(buffer []byte, xpathExpr string, baseUrl string) (ret []*XmlNode, err error) {
data := string(buffer)
ctxt, err := HtmlCreateMemoryParserCtxt(data)
if err != nil {
return
}
defer HtmlFreeParserCtxt(ctxt)
doc, err := HtmlCtxtReadDoc(ctxt, data, baseUrl, "", HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING)
if err != nil {
return
}
defer XmlFreeDoc(doc)
xpathCtx, err := XmlXPathNewContext(doc)
if err != nil {
return
}
defer XmlXPathFreeContext(xpathCtx)
xpathObj, err := XmlXPathEvalExpression(xpathExpr, xpathCtx)
if err != nil {
//fmt.Fprintf(os.Stderr,"Error: unable to evaluate xpath expression \"%s\"\n" , xpathExpr)
//return fmt.Errorf("error %d",1)
return
}
defer XmlXPathFreeObject(xpathObj)
ret = xpathObj.GetNodesetval()
return
}