Read a XML file in Go
Golang has a very powerful encoding/xml
package that comes with the standard library. All you need to do is the create the data structures to map according to XML document.
In this tutorial we will learn how to read this XML document with the xml.Unmarshal()
function
Employee.xml
<?xml version="1.0"?>
<company>
<staff>
<id>101</id>
<firstname>Derek</firstname>
<lastname>Young</lastname>
<username>derekyoung</username>
</staff>
<staff>
<id>102</id>
<firstname>John</firstname>
<lastname>Smith</lastname>
<username>johnsmith</username>
</staff>
</company>
and map to these data structs and print out the values
type Staff struct {
XMLName xml.Name `xml:"staff"`
ID int `xml:"id"`
FirstName string `xml:"firstname"`
LastName string `xml:"lastname"`
UserName string `xml:"username"`
}
type Company struct {
XMLName xml.Name `xml:"company"`
Staffs []Staff `xml:"staff"`
}
In real life, a company has many staffs. Therefore, we map Company.Staffs to Staff struct.
With the data structures defined. We are going to use the xml.Unmarshal()
function to load the XML data into the Company structure.
var c Company
xml.Unmarshal(XMLdata, &c)
Below is the full code of reading the XML file Employees.xml
package main
import (
"fmt"
"io/ioutil"
"os"
"encoding/xml"
)
type Staff struct {
XMLName xml.Name `xml:"staff"`
ID int `xml:"id"`
FirstName string `xml:"firstname"`
LastName string `xml:"lastname"`
UserName string `xml:"username"`
}
type Company struct {
XMLName xml.Name `xml:"company"`
Staffs []Staff `xml:"staff"`
}
func (s Staff) String() string {
return fmt.Sprintf("\t ID : %d - FirstName : %s - LastName : %s - UserName : %s \n", s.ID, s.FirstName , s.LastName, s.UserName)
}
func main() {
xmlFile, err := os.Open("Employees.xml")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer xmlFile.Close()
XMLdata, _ := ioutil.ReadAll(xmlFile)
var c Company
xml.Unmarshal(XMLdata, &c)
fmt.Println(c.Staffs)
}
and the output after parsing.
go run parseEmployee.go
ID : 101 - FirstName : Derek - LastName : Young - UserName : derekyoung
ID : 102 - FirstName : John - LastName : Smith - UserName : johnsmith
Hope this tutorial is helpful enough to you. Please leave your comment below if you have any question.
See also : Golang : How to read CSV file
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.8k Golang : Gomobile init produce "iphoneos" cannot be located error
+19.8k Golang : How to get time from unix nano example
+29.2k Golang : Save map/struct to JSON or XML file
+37.9k Golang : Read a text file and replace certain words
+7.5k Android Studio : AlertDialog to get user attention example
+5.3k Golang : What is StructTag and how to get StructTag's value?
+5.6k Golang : Struct field tags and what is their purpose?
+23.3k Golang : Read a file into an array or slice example
+9.2k Golang : Timeout example
+27.3k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+10.4k Generate Random number with math/rand in Go
+8k Golang : How To Use Panic and Recover