An XML file contains multiple elements with similar structures, but different names (e.g., PERSON and SENDER). The goal is to define a struct that allows for a dynamic element name.
While initially attempting to set the element name using the XMLName property wasn't successful, it is possible to achieve this by leveraging the following strategies:
type Person struct { XMLName xml.Name E1 string `xml:"ELEM1"` // ... }
person := Person { XMLName: xml.Name { Local: "Person" }, // ... }
Refer to the following playground example for a complete implementation: http://play.golang.org/p/bzSutFF9Bo.
The above is the detailed content of How Can I Marshal Dynamic XML Element Names in Go?. For more information, please follow other related articles on the PHP Chinese website!