XML to Go Struct Generator
Structs with encoding/xml tags, ready for xml.Unmarshal
XML Input
Go Output
Paste your XML on the left and the Go code shows up here.
Or drop a file on this box, , or .
What the generator writes
Paste an XML sample and you get Go structs tagged for the standard encoding/xml package. Attributes get `xml:"id,attr"`, an element's own text gets `xml:",chardata"`, children use their element name, and the root struct has an XMLName xml.Name field.
Repeated elements become slices, optional nested elements become pointers with omitempty, and leaf values are typed int, int64, float64, bool or string from the values you paste. Field names are exported PascalCase, as the spec requires for encoding/xml to see them. The output is gofmt-clean.
We ran go vet on the output and unmarshalled the page's sample with it before shipping. Call xml.Unmarshal(data, &sub) and you are done. Generation happens in your browser, so the XML is not uploaded.
How to use it
Paste a sample, set the package, copy the file.
Paste a sample
Paste a subscriber record into XML Input, or click Upload. A single <device> gives you a plain Device field; two of them give []Device, so paste a record where the list really repeats. Sample loads a telecom subscriber:
<subscriber id="SUB-1001" active="true">
<plan>Unlimited 5G</plan>
<device type="smartphone"><imei>356938035643809</imei></device>
<device type="tablet"><imei>490154203237518</imei></device>
</subscriber> Broken markup never reaches the Go side: the output panel prints the parser message instead of a half-written struct. Run the same text through the XML Validator to find the spot it trips on.
Set package and tags
Root struct overrides the name of the outer type; leave it blank and <subscriber> becomes Subscriber. Package fills in the package clause in lower case, and falls back to model if you type a Go keyword such as func. Keep encoding/xml tags ticked: untagged fields only match elements whose names equal the Go field name exactly, case included, and attributes are skipped.
Copy or download
Download saves a .go file; Copy puts it on the clipboard.
Where it helps
SOAP and partner APIs
Paste a response envelope and get structs you can Unmarshal into, instead of guessing tag syntax for attributes and text.
Config and manifest files
Maven POMs, Android manifests and vendor configs are XML. Typed structs make a CLI that reads them a short job.
Feeds
RSS, Atom and sitemap files map naturally onto slices of item structs.
Testing
Unmarshal golden files into structs and compare them with reflect.DeepEqual or go-cmp.
Common questions
Can I Unmarshal straight into the output?
Yes. That is what the tags are for. var sub Subscriber; err := xml.Unmarshal(data, &sub) fills the struct, slices included. See the Unmarshal docs for the matching rules.
How are attributes and text handled?
Attributes get ,attr, plus ,omitempty when some elements leave them out. An element holding both an attribute and text, say <msisdn type="mobile">447700900123</msisdn>, becomes its own struct with Type string beside Value int64 tagged ,chardata.
What types do leaf fields get?
int or int64 for whole numbers, float64 for decimals, bool for true/false, and string for everything else, including numbers with a leading zero. If a numeric field is really an ID (an IMEI, say), switch it to string.
How are namespaces handled?
Tags use the local name (`xml:"Body"` rather than soap:Body). encoding/xml then matches that name in any namespace, which is what you want in most cases.
Why are some nested structs pointers?
The element is missing from some records, so a pointer lets you tell "absent" from "empty". Types that contain themselves are pointers too.
Is the XML uploaded?
No. DOMParser reads it inside this tab and the Go source is assembled there too, so nothing goes over the network. Pastes above 256 KB are refused, which costs you nothing: once every element has appeared, more records only repeat fields the structs already have.
Related tools
Other converters you might reach for next: