Last active
February 21, 2018 00:43
-
-
Save larryprice/204fec2e8d33979f8cac to your computer and use it in GitHub Desktop.
Sample code used for blog post on parsing XML with Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sample code used for blog post on parsing XML with Go | |
// https://larry-price.com | |
// | |
// Go Playground: https://play.golang.org/p/qiSoxxb5tp | |
package main | |
import ( | |
"encoding/xml" | |
"fmt" | |
) | |
var personXML = []byte(` | |
<person> | |
<name>Luann Van Houten</name> | |
<addresses> | |
<address type="secondary"> | |
<street>321 MadeUp Lane</street> | |
<city>Shelbyville</city> | |
</address> | |
<address type="primary"> | |
<street>123 Fake St</street> | |
<city>Springfield</city> | |
</address> | |
</addresses> | |
</person>`) | |
type Person struct { | |
Name string `xml:"name"` | |
Addresses []struct { | |
Street string `xml:"street"` | |
City string `xml:"city"` | |
Type string `xml:"type,attr"` | |
} `xml:"addresses>address"` | |
} | |
func main() { | |
var luann Person | |
xml.Unmarshal(personXML, &luann) | |
fmt.Println(luann) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment