XML to Go
Turn an XML sample into go structs: one type per element, attributes as fields, repeated child elements as lists.
What the XML to Go does
An element with children is an object; an element with only text is a string. A name that appears more than once among the children is a list of that element's shape, which is how XML says "repeated" without a schema.
Attributes are attributes carry the ,attr tag flag, because they are part of the element rather than a child of it.
Text content is not modelled: an element with children contributes its children, not its mixed text. Namespaces are kept as they appear in the tag name, so a prefixed element produces a prefixed field name that may need rewriting.
How to use it
Paste the XML into the XML sample box, or press Run with the example already there.
Read the go structs below: the root element is the last type, and each child element type appears before it.
Check a repeated element in the Go structs — it is a list because the same name appears more than once among the siblings.
Worked example
Input:
<user id="7"><name>Ada</name><address><city>London</city></address><tag>a</tag><tag>b</tag></user>
Output:
type Address struct {
City string `xml:"city"`
}
type User struct {
Id string `xml:"id,attr"`
Name string `xml:"name"`
Address Address `xml:"address"`
Tag []string `xml:"tag"`
}
Why it runs in your browser
The result is produced on this page with JavaScript, so it works offline after the page loads and nothing you enter is sent anywhere.
A shape read from the element tree: the root element names the first type, its attributes become fields, and a child that repeats becomes a list.
The page parses the document before it emits anything, so a tag closed in the wrong place is refused with its line number instead of producing a type tree that silently disagrees with the file.
What it does, and what it does not
An element with children is an object; an element with only text is a string. A name that appears more than once among the children is a list of that element's shape, which is how XML says "repeated" without a schema.
Attributes are attributes carry the `,attr` tag flag, because they are part of the element rather than a child of it.
Text content is not modelled: an element with children contributes its children, not its mixed text. Namespaces are kept as they appear in the tag name, so a prefixed element produces a prefixed field name that may need rewriting.
How to use XML to Go
Paste the XML into the XML sample box, or press Run with the example already there.
Read the go structs below: the root element is the last type, and each child element type appears before it.
Check a repeated element in the Go structs — it is a list because the same name appears more than once among the siblings.
What to Expect
Best for
- Starting Go types from a real response body
- Seeing how attributes map into Go
Not ideal for
- Mixed content, where text and elements interleave
- A schema-driven mapping, which needs an XSD
What this tool keeps
- One type per element, named from the tag
- A repeated child as a list
What may need cleanup
- A namespace prefix stays in the field name
- Mixed text is dropped, so a prose element needs hand work
Common errors
- The page refuses it: </user> closes <name>
Example use cases
- Typing a SOAP or REST response in Go
- Checking an element shape before writing the structs
Sample input
XML sample: <user id="7"><name>Ada</name><address><city>London</city></address><tag>a</tag><tag>b</tag></user>
Sample output
Go structs: type Address struct { …
Who this is for
- Go developers consuming an XML API
- Anyone porting an XML document into typed code
Frequently asked questions
Why is a repeated element a list?
More than one child with the same name is how XML expresses repetition without a schema. One occurrence is a single value; two or more become a list of that element's shape.
What happens to text inside an element that also has children?
It is dropped. A mixed-content element has no single shape, and inventing one would misrepresent the document; the page models elements and attributes only.
Are namespaces resolved?
No. The prefix stays in the tag name and therefore in the type name, so soap:Body becomes a field named after the prefixed tag. Rename it where your code needs it.
Build custom tools for your store
Join the SDK program to create useful ecommerce utilities for your store.