Quick actions

cmd+k|ctrl+k

Navigation

Languages

Essential Go / XML

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T10:02:16Z

Updated

2019-03-26T10:02:16Z

package main

import (
	"encoding/xml"
	"fmt"
	"log"
)

var xmlStr = `
<people>
	<person age="34">
		<first-name>John</first-name>
		<address>
			<city>San Francisco</city>
			<state>CA</state>
		</address>
	</person>

	<person age="23">
		<first-name>Julia</first-name>
	</person>
</people>`

type People struct {
	Person []Person `xml:"person"`
}

type Person struct {
	Age       int     `xml:"age,attr"`
	FirstName string  `xml:"first-name"`
	Address   Address `xml:"address"`
}

type Address struct {
	City  *string `xml:"city"`
	State string  `xml:"state"`
}

func main() {
	var people People
	data := []byte(xmlStr)
	err := xml.Unmarshal(data, &people)
	if err != nil {
		log.Fatalf("xml.Unmarshal failed with '%s'\n", err)
	}
	fmt.Printf("%#v\n\n", people)
}
INFO