forked from beevik/etree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
64 lines (54 loc) · 1.66 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
// Copyright 2013 Brett Vickers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package etree_test
import (
"github.com/beevik/etree"
"os"
)
// Create an etree Document, add XML entities to it, and serialize it
// to stdout.
func ExampleDocument_creating() {
doc := etree.NewDocument()
doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
doc.CreateProcInst("xml-stylesheet", `type="text/xsl" href="style.xsl"`)
people := doc.CreateElement("People")
people.CreateComment("These are all known people")
jon := people.CreateElement("Person")
jon.CreateAttr("name", "Jon")
sally := people.CreateElement("Person")
sally.CreateAttr("name", "Sally")
doc.Indent(2)
doc.WriteTo(os.Stdout)
// Output:
// <?xml version="1.0" encoding="UTF-8"?>
// <?xml-stylesheet type="text/xsl" href="style.xsl"?>
// <People>
// <!--These are all known people-->
// <Person name="Jon"/>
// <Person name="Sally"/>
// </People>
}
func ExampleDocument_reading() {
doc := etree.NewDocument()
if err := doc.ReadFromFile("document.xml"); err != nil {
panic(err)
}
}
func ExamplePath() {
xml := `<bookstore><book><title>Great Expectations</title>
<author>Charles Dickens</author></book><book><title>Ulysses</title>
<author>James Joyce</author></book></bookstore>`
doc := etree.NewDocument()
doc.ReadFromString(xml)
for _, e := range doc.FindElements(".//book[author='Charles Dickens']") {
book := etree.CreateDocument(e)
book.Indent(2)
book.WriteTo(os.Stdout)
}
// Output:
// <book>
// <title>Great Expectations</title>
// <author>Charles Dickens</author>
// </book>
}