Advertisement728 × 90 — Leaderboard
Advertisement320 × 50 — Mobile banner

XML to Go

Generate Go structs from XML with encoding/xml tags
Session restored from your last visit
Also need:XML to JSONJSON to Go StructXML Formatter
Advertisement — Fullscreen
Input
Output
Input
AdvertisementResponsive
Output
Ready0 chars0 lines
Advertisement728 × 90

About XML to Go

Paste your XML and get Go struct definitions with proper encoding/xml struct tags. Attributes get ,attr tags, text content gets ,chardata.

Frequently Asked Questions

What is XML to Go struct generation?
XML to Go struct generation analyses an XML document and produces Go struct definitions with xml:"..." field tags compatible with encoding/xml. The structs can be used directly with xml.Unmarshal() to deserialise XML into typed Go data structures.
How do I add xml struct tags for XML attributes in Go?
Use the ,attr flag in the xml tag: ID int `xml:"id,attr"`. Without ,attr, the field is treated as a child element. For the element's text content, use xml:",chardata". For inner XML as a raw string, use xml:",innerxml". The generated struct includes these tags; verify them against your XML document before using.
How do I unmarshal an XML document into a Go struct using encoding/xml?
var result YourStruct; err := xml.Unmarshal([]byte(xmlData), &result). For files: f, _ := os.Open("file.xml"); xml.NewDecoder(f).Decode(&result). The root element tag in the XML must match the XMLName field or the struct's xml tag, otherwise unmarshal silently produces zero values.
How are XML namespaces represented in Go struct tags?
Include the namespace URI in the xml tag using space syntax: `xml:"http://namespace.uri localname"`. Example: `xml:"http://www.w3.org/1999/xhtml body"`. For attributes: `xml:"http://namespace.uri localname,attr"`. Go's encoding/xml is stricter about namespace matching than most other XML libraries.
What is the difference between XMLName and the xml struct tag in Go?
XMLName xml.Name `xml:"tagname"` specifies the element name for the struct at the type level and is used during both marshal and unmarshal. Using `xml:"tagname"` on a struct field specifies the element name at the field level. Both approaches work; XMLName is clearer when the struct represents a root element with a specific tag.
Advertisement300 × 250
Advertisement300 × 250
Ad
AdvertisementSticky banner
Ad
AdvertisementSticky banner