package main import ( "encoding/xml" "fmt" "strings" ) func main() { data := `` decoder := xml.NewDecoder(strings.NewReader(data)) for { token, err := decoder.Token() if err != nil { break } switch t := token.(type) { case xml.StartElement: fmt.Printf("start element: %s\n", t.Name.Local) case xml.EndElement: fmt.Printf("end element: %s\n", t.Name.Local) case xml.CharData: fmt.Printf("char data: %s\n", string(t)) } } } John Doe 30
package main import ( "encoding/xml" "fmt" "strings" ) type Person struct { Name string `xml:"name"` Age int `xml:"age,attr"` } func main() { data := `In this example, we define a Person struct with two fields - Name and Age. We use xml tags to specify how the fields should be mapped to the XML document. We then create a new decoder object and use the Decode method to decode the XML document into our Person struct. Overall, the encoding/xml Decoder Token is a useful type for decoding and manipulating XML data in Go.` decoder := xml.NewDecoder(strings.NewReader(data)) var person Person err := decoder.Decode(&person) if err != nil { fmt.Println(err) return } fmt.Printf("Name: %s, Age: %d\n", person.Name, person.Age) } John Doe