# Reading XML Documents in Go

DevFeed: [Reading XML Documents in Go](<https://devfeed.tech/articles/reading-xml-documents-in-go-22050.md>)

Original publisher: [Read original article](<https://www.ardanlabs.com/blog/2013/06/reading-xml-documents-in-go.html>)

Published: 2013-06-17T00:00:00Z

Content type: tutorial

Language: en

Sources: [William Kennedy](<https://devfeed.tech/sources/william-kennedy.md>)

Topics: [Go Language](<https://devfeed.tech/topics/go-language.md>), [XML](<https://devfeed.tech/topics/xml.md>), [Code](<https://devfeed.tech/topics/code.md>), [Library](<https://devfeed.tech/topics/library.md>)

Tags: [ardan-labs](<https://devfeed.tech/tags/ardan-labs.md>), [blog](<https://devfeed.tech/tags/blog.md>), [code](<https://devfeed.tech/tags/code.md>), [decoding](<https://devfeed.tech/tags/decoding.md>), [function](<https://devfeed.tech/tags/function.md>), [go](<https://devfeed.tech/tags/go.md>), [go-programming](<https://devfeed.tech/tags/go-programming.md>), [golang](<https://devfeed.tech/tags/golang.md>), [io](<https://devfeed.tech/tags/io.md>), [io-reader](<https://devfeed.tech/tags/io-reader.md>), [library](<https://devfeed.tech/tags/library.md>), [programming](<https://devfeed.tech/tags/programming.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>), [xml](<https://devfeed.tech/tags/xml.md>)

## AI overview

A tutorial on reading and deserializing XML documents in Go using the standard library's encoding/xml package. It explains how to define structs and XML tags, decode through an io.Reader, and return parsed nodes, while also mentioning xmlpath for greater flexibility.

## Source excerpt

I was really surprised how easy it was to read an XML document using the encoding/xml package that comes with the standard library. The package works by defining structs that map the XML document. If you need more flexibility then use Gustavo Niemeyer's xmlpath package (found here). Here is the XML document we are going to read and de-serialize: <straps> <strap key="CompanyName" value="NEWCO" /> <strap key="UseEmail" value="true" /> </straps> The first thing we need to do is define the structs we will use to map the document: type XMLStrap struct { XMLName xml.Name ̀ xml:"strap"̀ Key string ̀ xml:"key,attr"̀ Value string ̀ xml:"value,attr"̀ } type XMLStraps struct { XMLName xml.Name ̀ xml:"straps"̀ Straps []XMLStrap ̀ xml:"strap"̀ } There are two structs, one for the entire document (<straps>) and one for each individual child node (<strap>). If you look closely at the structs you may see something new. Each field has a tag associated with it. These tags are bound to each individual field. Go's reflect package allows you to access these tags. These tag formats are specific to the decoding support inside the encoding/xml package. The tags map the nodes and attributes of the XML document to the struct. The following code decodes the XML document and returns the array of strap nodes: func ReadStraps(reader io.Reader) ([]XMLStrap, error) { var xmlStraps XMLStraps if err := xml.NewDecoder(reader).Decode(&xmlStraps); err != nil { return nil, err } return xmlStraps.Straps, nil } The function takes an io.Reader. We will be passing a os.File variable into this method. The function returns an array of pointers for each strap we read from the file. First we create a XMLStraps variable and get its address. Next we create a decoder using the xml.NewDecoder method passing the io.Reader object. Then we call Decode which reads the file and de-serializes the file into the XMLStraps variable. Then we just return the array of strap values. The following completes the sample code: /* s