package main import ( "encoding/xml" "fmt" "os" ) type Person struct { Name string `xml:"name"` Email string `xml:"email"` Mobile string `xml:"mobile"` } func main() { p := Person{"Alice", "[email protected]", "123-456-7890"} enc := xml.NewEncoder(os.Stdout) enc.Encode(p) }
package main import ( "encoding/xml" "fmt" "strings" ) type Person struct { Name string `xml:"name"` Email string `xml:"email"` Mobile string `xml:"mobile"` } func main() { xmlData := `In this example, we define a `Person` struct and decode XML data from a string using the `xml.NewDecoder()` function. We use the `dec.Token()` function to iterate through the tokens in the XML data and decode the `Person` struct when we encounter the `` dec := xml.NewDecoder(strings.NewReader(xmlData)) for { t, err := dec.Token() if err != nil { break } switch se := t.(type) { case xml.StartElement: if se.Name.Local == "person" { var p Person dec.DecodeElement(&p, &se) fmt.Printf("%+v\n", p) } } } } Alice [email protected] 123-456-7890