-
Notifications
You must be signed in to change notification settings - Fork 11
/
xml_wrapper.go
98 lines (78 loc) · 2.2 KB
/
xml_wrapper.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package golibxml
/*
#cgo pkg-config: libxml-2.0
#include <libxml/tree.h>
static inline void free_string(char* s) { free(s); }
static inline xmlChar *to_xmlcharptr(const char *s) { return (xmlChar *)s; }
static inline char *to_charptr(const xmlChar *s) { return (char *)s; }
*/
import "C"
import "unsafe"
func (doc *Document) String() string {
buf := NewBuffer()
defer buf.Free()
doc.NodeDump(buf, doc.Node, 0, 0)
return buf.Content()
}
func (node *Node) Document() *Document {
return makeDoc(C.xmlDocPtr(unsafe.Pointer(node.Ptr.doc)))
}
func (node *Node) String() string {
buf := NewBuffer()
defer buf.Free()
node.Document().NodeDump(buf, node, 0, 0)
return buf.Content()
}
func (node *Node) Children() *Node {
return makeNode(C.xmlNodePtr(unsafe.Pointer(node.Ptr.children)))
}
func (node *Node) Type() ElementType {
return ElementType(node.Ptr._type)
}
func (node *Node) Name() string {
return C.GoString(C.to_charptr(node.Ptr.name))
}
func (node *Node) Next() *Node {
return makeNode(C.xmlNodePtr(unsafe.Pointer(node.Ptr.next)))
}
func (node *Node) Attributes() *Attribute {
return makeAttribute(C.xmlAttrPtr(unsafe.Pointer(node.Ptr.properties)))
}
func (node *Node) Namespace() *Namespace {
return makeNamespace(C.xmlNsPtr(unsafe.Pointer(node.Ptr.ns)))
}
func (attr *Attribute) Type() ElementType {
return ElementType(attr.Ptr._type)
}
func (attr *Attribute) Name() string {
return C.GoString(C.to_charptr(attr.Ptr.name))
}
func (attr *Attribute) Children() *Node {
return makeNode(C.xmlNodePtr(unsafe.Pointer(attr.Ptr.children)))
}
func (attr *Attribute) Next() *Attribute {
return makeAttribute(C.xmlAttrPtr(unsafe.Pointer(attr.Ptr.next)))
}
func (attr *Attribute) Namespace() *Namespace {
return makeNamespace(C.xmlNsPtr(unsafe.Pointer(attr.Ptr.ns)))
}
func (ns *Namespace) Href() string {
return C.GoString(C.to_charptr(ns.Ptr.href))
}
func (ns *Namespace) Prefix() string {
return C.GoString(C.to_charptr(ns.Ptr.prefix))
}
func (elem ElementType) GoString() string {
return elem.String()
}
func (elem ElementType) String() string {
switch elem {
case XML_ELEMENT_NODE:
return "Node"
case XML_ATTRIBUTE_NODE:
return "Attribute"
case XML_TEXT_NODE:
return "Text"
}
return "Unknown Type"
}