XML Input

Go Output

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.

1

Paste a sample

Paste XML into XML Input or Upload a file. Repeated elements must appear twice to become a slice. 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>

Parse errors appear in the output panel; the XML Validator shows exactly where.

2

Set package and tags

Root struct renames the top-level type (empty uses the root element name). Package sets the package clause. Leave encoding/xml tags on; without tags, encoding/xml only matches elements whose names exactly equal the Go field names (case included), and it ignores attributes.

3

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. If an element has attributes and text, like <price currency="USD">24.99</price>, the text goes into a Value field 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. It is parsed in your browser. Inputs over 256 KB are rejected.

Related tools

Other converters you might reach for next: