• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.travis.ymlH A D02-Feb-2019137

CONTRIBUTORSH A D02-Feb-2019264

LICENSEH A D02-Feb-20191.3 KiB

README.mdH A D02-Feb-20195.5 KiB

RELEASE_NOTES.mdH A D02-Feb-20194.7 KiB

etree.goH A D02-Feb-201936.6 KiB

etree_test.goH A D02-Feb-201928.9 KiB

example_test.goH A D02-Feb-20191.6 KiB

helpers.goH A D02-Feb-20195.6 KiB

path.goH A D02-Feb-201915.5 KiB

path_test.goH A D02-Feb-20196.5 KiB

README.md

1[![Build Status](https://travis-ci.org/beevik/etree.svg?branch=master)](https://travis-ci.org/beevik/etree)
2[![GoDoc](https://godoc.org/github.com/beevik/etree?status.svg)](https://godoc.org/github.com/beevik/etree)
3
4etree
5=====
6
7The etree package is a lightweight, pure go package that expresses XML in
8the form of an element tree.  Its design was inspired by the Python
9[ElementTree](http://docs.python.org/2/library/xml.etree.elementtree.html)
10module.
11
12Some of the package's capabilities and features:
13
14* Represents XML documents as trees of elements for easy traversal.
15* Imports, serializes, modifies or creates XML documents from scratch.
16* Writes and reads XML to/from files, byte slices, strings and io interfaces.
17* Performs simple or complex searches with lightweight XPath-like query APIs.
18* Auto-indents XML using spaces or tabs for better readability.
19* Implemented in pure go; depends only on standard go libraries.
20* Built on top of the go [encoding/xml](http://golang.org/pkg/encoding/xml)
21  package.
22
23### Creating an XML document
24
25The following example creates an XML document from scratch using the etree
26package and outputs its indented contents to stdout.
27```go
28doc := etree.NewDocument()
29doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
30doc.CreateProcInst("xml-stylesheet", `type="text/xsl" href="style.xsl"`)
31
32people := doc.CreateElement("People")
33people.CreateComment("These are all known people")
34
35jon := people.CreateElement("Person")
36jon.CreateAttr("name", "Jon")
37
38sally := people.CreateElement("Person")
39sally.CreateAttr("name", "Sally")
40
41doc.Indent(2)
42doc.WriteTo(os.Stdout)
43```
44
45Output:
46```xml
47<?xml version="1.0" encoding="UTF-8"?>
48<?xml-stylesheet type="text/xsl" href="style.xsl"?>
49<People>
50  <!--These are all known people-->
51  <Person name="Jon"/>
52  <Person name="Sally"/>
53</People>
54```
55
56### Reading an XML file
57
58Suppose you have a file on disk called `bookstore.xml` containing the
59following data:
60
61```xml
62<bookstore xmlns:p="urn:schemas-books-com:prices">
63
64  <book category="COOKING">
65    <title lang="en">Everyday Italian</title>
66    <author>Giada De Laurentiis</author>
67    <year>2005</year>
68    <p:price>30.00</p:price>
69  </book>
70
71  <book category="CHILDREN">
72    <title lang="en">Harry Potter</title>
73    <author>J K. Rowling</author>
74    <year>2005</year>
75    <p:price>29.99</p:price>
76  </book>
77
78  <book category="WEB">
79    <title lang="en">XQuery Kick Start</title>
80    <author>James McGovern</author>
81    <author>Per Bothner</author>
82    <author>Kurt Cagle</author>
83    <author>James Linn</author>
84    <author>Vaidyanathan Nagarajan</author>
85    <year>2003</year>
86    <p:price>49.99</p:price>
87  </book>
88
89  <book category="WEB">
90    <title lang="en">Learning XML</title>
91    <author>Erik T. Ray</author>
92    <year>2003</year>
93    <p:price>39.95</p:price>
94  </book>
95
96</bookstore>
97```
98
99This code reads the file's contents into an etree document.
100```go
101doc := etree.NewDocument()
102if err := doc.ReadFromFile("bookstore.xml"); err != nil {
103    panic(err)
104}
105```
106
107You can also read XML from a string, a byte slice, or an `io.Reader`.
108
109### Processing elements and attributes
110
111This example illustrates several ways to access elements and attributes using
112etree selection queries.
113```go
114root := doc.SelectElement("bookstore")
115fmt.Println("ROOT element:", root.Tag)
116
117for _, book := range root.SelectElements("book") {
118    fmt.Println("CHILD element:", book.Tag)
119    if title := book.SelectElement("title"); title != nil {
120        lang := title.SelectAttrValue("lang", "unknown")
121        fmt.Printf("  TITLE: %s (%s)\n", title.Text(), lang)
122    }
123    for _, attr := range book.Attr {
124        fmt.Printf("  ATTR: %s=%s\n", attr.Key, attr.Value)
125    }
126}
127```
128Output:
129```
130ROOT element: bookstore
131CHILD element: book
132  TITLE: Everyday Italian (en)
133  ATTR: category=COOKING
134CHILD element: book
135  TITLE: Harry Potter (en)
136  ATTR: category=CHILDREN
137CHILD element: book
138  TITLE: XQuery Kick Start (en)
139  ATTR: category=WEB
140CHILD element: book
141  TITLE: Learning XML (en)
142  ATTR: category=WEB
143```
144
145### Path queries
146
147This example uses etree's path functions to select all book titles that fall
148into the category of 'WEB'.  The double-slash prefix in the path causes the
149search for book elements to occur recursively; book elements may appear at any
150level of the XML hierarchy.
151```go
152for _, t := range doc.FindElements("//book[@category='WEB']/title") {
153    fmt.Println("Title:", t.Text())
154}
155```
156
157Output:
158```
159Title: XQuery Kick Start
160Title: Learning XML
161```
162
163This example finds the first book element under the root bookstore element and
164outputs the tag and text of each of its child elements.
165```go
166for _, e := range doc.FindElements("./bookstore/book[1]/*") {
167    fmt.Printf("%s: %s\n", e.Tag, e.Text())
168}
169```
170
171Output:
172```
173title: Everyday Italian
174author: Giada De Laurentiis
175year: 2005
176price: 30.00
177```
178
179This example finds all books with a price of 49.99 and outputs their titles.
180```go
181path := etree.MustCompilePath("./bookstore/book[p:price='49.99']/title")
182for _, e := range doc.FindElementsPath(path) {
183    fmt.Println(e.Text())
184}
185```
186
187Output:
188```
189XQuery Kick Start
190```
191
192Note that this example uses the FindElementsPath function, which takes as an
193argument a pre-compiled path object. Use precompiled paths when you plan to
194search with the same path more than once.
195
196### Other features
197
198These are just a few examples of the things the etree package can do. See the
199[documentation](http://godoc.org/github.com/beevik/etree) for a complete
200description of its capabilities.
201
202### Contributing
203
204This project accepts contributions. Just fork the repo and submit a pull
205request!
206