-
Notifications
You must be signed in to change notification settings - Fork 30
/
example_test.go
156 lines (137 loc) · 5.06 KB
/
example_test.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package xmldom_test
import (
"fmt"
"github.com/subchen/go-xmldom"
)
const (
ExampleXml = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE junit SYSTEM "junit-result.dtd">
<testsuites>
<testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom">
<properties>
<property name="go.version">go1.8.1</property>
</properties>
<testcase classname="go-xmldom" id="ExampleParseXML" time="0.004"></testcase>
<testcase classname="go-xmldom" id="ExampleParse" time="0.005"></testcase>
<testcase xmlns:test="mock" id="AttrNamespace"></testcase>
</testsuite>
</testsuites>`
)
func ExampleParseXML() {
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
fmt.Printf("name = %v\n", node.Name)
fmt.Printf("attributes.len = %v\n", len(node.Attributes))
fmt.Printf("children.len = %v\n", len(node.Children))
fmt.Printf("root = %v", node == node.Root())
// Output:
// name = testsuites
// attributes.len = 0
// children.len = 1
// root = true
}
func ExampleNode_GetAttribute() {
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
attr := node.FirstChild().GetAttribute("name")
fmt.Printf("%v = %v\n", attr.Name, attr.Value)
// Output:
// name = github.com/subchen/go-xmldom
}
func ExampleNode_GetChildren() {
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
children := node.FirstChild().GetChildren("testcase")
for _, c := range children {
fmt.Printf("%v: id = %v\n", c.Name, c.GetAttributeValue("id"))
}
// Output:
// testcase: id = ExampleParseXML
// testcase: id = ExampleParse
// testcase: id = AttrNamespace
}
func ExampleNode_FindByID() {
root := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
node := root.FindByID("ExampleParseXML")
fmt.Println(node.XML())
// Output:
// <testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" />
}
func ExampleNode_FindOneByName() {
root := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
node := root.FindOneByName("property")
fmt.Println(node.XML())
// Output:
// <property name="go.version">go1.8.1</property>
}
func ExampleNode_FindByName() {
root := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
nodes := root.FindByName("testcase")
for _, node := range nodes {
fmt.Println(node.XML())
}
// Output:
// <testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" />
// <testcase classname="go-xmldom" id="ExampleParse" time="0.005" />
// <testcase xmlns:test="mock" id="AttrNamespace" />
}
func ExampleNode_Query() {
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
// xpath expr: https://github.com/antchfx/xpath
// find all children
fmt.Printf("children = %v\n", len(node.Query("//*")))
// find node matched tag name
nodeList := node.Query("//testcase")
for _, c := range nodeList {
fmt.Printf("%v: id = %v\n", c.Name, c.GetAttributeValue("id"))
}
// Output:
// children = 6
// testcase: id = ExampleParseXML
// testcase: id = ExampleParse
// testcase: id = AttrNamespace
}
func ExampleNode_QueryOne() {
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
// xpath expr: https://github.com/antchfx/xpath
// find node matched attr name
c := node.QueryOne("//testcase[@id='ExampleParseXML']")
fmt.Printf("%v: id = %v\n", c.Name, c.GetAttributeValue("id"))
// Output:
// testcase: id = ExampleParseXML
}
func ExampleDocument_XML() {
doc := xmldom.Must(xmldom.ParseXML(ExampleXml))
fmt.Println(doc.XML())
// Output:
// <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE junit SYSTEM "junit-result.dtd"><testsuites><testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom"><properties><property name="go.version">go1.8.1</property></properties><testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" /><testcase classname="go-xmldom" id="ExampleParse" time="0.005" /><testcase xmlns:test="mock" id="AttrNamespace" /></testsuite></testsuites>
}
func ExampleDocument_XMLPretty() {
doc := xmldom.Must(xmldom.ParseXML(ExampleXml))
fmt.Println(doc.XMLPretty())
// Output:
// <?xml version="1.0" encoding="UTF-8"?>
// <!DOCTYPE junit SYSTEM "junit-result.dtd">
// <testsuites>
// <testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom">
// <properties>
// <property name="go.version">go1.8.1</property>
// </properties>
// <testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" />
// <testcase classname="go-xmldom" id="ExampleParse" time="0.005" />
// <testcase xmlns:test="mock" id="AttrNamespace" />
// </testsuite>
// </testsuites>
}
func ExampleNewDocument() {
doc := xmldom.NewDocument("testsuites")
testsuiteNode := doc.Root.CreateNode("testsuite").SetAttributeValue("name", "github.com/subchen/go-xmldom")
testsuiteNode.CreateNode("testcase").SetAttributeValue("name", "case 1").Text = "PASS"
testsuiteNode.CreateNode("testcase").SetAttributeValue("name", "case 2").Text = "FAIL"
fmt.Println(doc.XMLPretty())
// Output:
// <?xml version="1.0" encoding="UTF-8"?>
// <testsuites>
// <testsuite name="github.com/subchen/go-xmldom">
// <testcase name="case 1">PASS</testcase>
// <testcase name="case 2">FAIL</testcase>
// </testsuite>
// </testsuites>
}