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