package main import ( "encoding/xml" "fmt" "os" ) type User struct { Name string `xml:"name"` Email string `xml:"email"` } func main() { file, err := os.Open("users.xml") if err != nil { fmt.Println("Error:", err) return } defer file.Close() decoder := xml.NewDecoder(file) for { token, err := decoder.Token() if err != nil { fmt.Println("Error:", err) return } if element, ok := token.(xml.StartElement); ok { fmt.Println(element.Name.Local) } } }
package main import ( "encoding/xml" "fmt" "os" ) type User struct { Name string `xml:"name"` Email string `xml:"email"` } func main() { file, err := os.Open("user.xml") if err != nil { fmt.Println("Error:", err) return } defer file.Close() var user User decoder := xml.NewDecoder(file) err = decoder.Decode(&user) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Name: %s\nEmail: %s", user.Name, user.Email) }In this example, we define a struct `User` with two fields, `Name` and `Email`, annotated with XML tags. We then open an XML file, create a new decoder to read from it, and decode the contents of the file into a `User` struct. Finally, we print the values of the `Name` and `Email` fields to the console. Package library used: encoding/xml