Last active
August 29, 2015 14:07
-
-
Save mickelsonm/3c17fff2b33c5ad5b42a to your computer and use it in GitHub Desktop.
Golang XML Parsing Example - http://play.golang.org/p/Qewm4sEoZ6
This file contains hidden or 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
package main | |
import( | |
"encoding/xml" | |
"fmt" | |
"strings" | |
) | |
var text = ` | |
<people> | |
<person id="10"> | |
<name>Fred Flintstone</name> | |
<address> | |
<city>Bedrock</city> | |
<state>CA</state> | |
</address> | |
</person> | |
<person id="11"> | |
<name>Will Farrell</name> | |
<address> | |
<city>Los Angeles</city> | |
<state>CA</state> | |
</address> | |
</person> | |
<person id="12"> | |
<name>Vince Vaughn</name> | |
<address> | |
<city>Los Angeles</city> | |
<state>CA</state> | |
</address> | |
</person> | |
<person id="13"> | |
<name>Alexander Pavlov</name> | |
<address> | |
<city>Madison</city> | |
<state>WI</state> | |
</address> | |
</person> | |
<person id="14"> | |
<name>Michael Connley</name> | |
<address> | |
<city>Eau Claire</city> | |
<state>WI</state> | |
</address> | |
</person> | |
</people>` | |
type Results struct{ | |
People []Person `xml:"person"` | |
} | |
type People []Person | |
type Person struct { | |
Id int `xml:"id,attr"` | |
Name string `xml:"name"` | |
Address Address `xml:"address"` | |
} | |
type Address struct{ | |
City string `xml:"city"` | |
State string `xml:"state"` | |
} | |
func main() { | |
results := new(Results) | |
xml.NewDecoder(strings.NewReader(text)).Decode(results) | |
for _, p := range results.People{ | |
fmt.Printf("person = %+v\n", p) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment