forked from ChrisTrenkamp/goxpath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlnode.go
78 lines (67 loc) · 1.82 KB
/
xmlnode.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
package xmlnode
import (
"encoding/xml"
"github.com/bserdar/goxpath/tree"
)
//Node is a XPath result that is a node except elements
type Node interface {
//ResValue prints the node's string value
ResValue() string
//Pos returns the node's position in the document order
Pos() int
//GetToken returns the xml.Token representation of the node
GetToken() xml.Token
//GetParent returns the parent node, which will always be an XML element
GetParent() Elem
//GetNodeType returns the node's type
GetNodeType() tree.NodeType
}
//Elem is a XPath result that is an element node
type Elem interface {
Node
//GetChildren returns the elements children.
GetChildren() []Node
//GetAttrs returns the attributes of the element
GetAttrs() []Node
}
//NSElem is a node that keeps track of namespaces.
type NSElem interface {
Elem
GetNS() map[xml.Name]string
}
//XMLNode will represent an attribute, character data, comment, or processing instruction node
type XMLNode struct {
xml.Token
tree.NodeType
Parent Elem
Position int
}
// Pos returns node position
func (a XMLNode) Pos() int { return a.Position }
// GetNodeType returns the node type
func (a XMLNode) GetNodeType() tree.NodeType { return a.NodeType }
//GetToken returns the xml.Token representation of the node
func (a XMLNode) GetToken() xml.Token {
if a.NodeType == tree.NtAttr {
ret := a.Token.(*xml.Attr)
return *ret
}
return a.Token
}
//GetParent returns the parent node
func (a XMLNode) GetParent() Elem {
return a.Parent
}
//ResValue returns the string value of the attribute
func (a XMLNode) ResValue() string {
switch a.NodeType {
case tree.NtAttr:
return a.Token.(*xml.Attr).Value
case tree.NtChd:
return string(a.Token.(xml.CharData))
case tree.NtComm:
return string(a.Token.(xml.Comment))
}
//case tree.NtPi:
return string(a.Token.(xml.ProcInst).Inst)
}