Debian Codegraph @

example_test.go

raw symbols SCIP
1 // Copyright 2015-2019 Brett Vickers.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package etree
6
7 import "os"
8
9 // Create an etree Document, add XML entities to it, and serialize it
10 // to stdout.
11 func ExampleDocument_creating() {
12 doc := NewDocument()
13 doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
14 doc.CreateProcInst("xml-stylesheet", `type="text/xsl" href="style.xsl"`)
15
16 people := doc.CreateElement("People")
17 people.CreateComment("These are all known people")
18
19 jon := people.CreateElement("Person")
20 jon.CreateAttr("name", "Jon O'Reilly")
21
22 sally := people.CreateElement("Person")
23 sally.CreateAttr("name", "Sally")
24
25 doc.Indent(2)
26 doc.WriteTo(os.Stdout)
27 // Output:
28 // <?xml version="1.0" encoding="UTF-8"?>
29 // <?xml-stylesheet type="text/xsl" href="style.xsl"?>
30 // <People>
31 // <!--These are all known people-->
32 // <Person name="Jon O&apos;Reilly"/>
33 // <Person name="Sally"/>
34 // </People>
35 }
36
37 func ExampleDocument_reading() {
38 doc := NewDocument()
39 if err := doc.ReadFromFile("document.xml"); err != nil {
40 panic(err)
41 }
42 }
43
44 func ExamplePath() {
45 xml := `
46 <bookstore>
47 <book>
48 <title>Great Expectations</title>
49 <author>Charles Dickens</author>
50 </book>
51 <book>
52 <title>Ulysses</title>
53 <author>James Joyce</author>
54 </book>
55 </bookstore>`
56
57 doc := NewDocument()
58 doc.ReadFromString(xml)
59 for _, e := range doc.FindElements(".//book[author='Charles Dickens']") {
60 doc := NewDocumentWithRoot(e.Copy())
61 doc.Indent(2)
62 doc.WriteTo(os.Stdout)
63 }
64 // Output:
65 // <book>
66 // <title>Great Expectations</title>
67 // <author>Charles Dickens</author>
68 // </book>
69 }