FormatterHQ

XML to Go Struct Generator

Generate Go structs from an XML sample. Attributes, child elements and element text become typed fields; switch the json tags to xml tags for encoding/xml.

Input XML
Output GO
Ready.

How to use the XML to Go

  1. Paste your XML document into the Input editor, or use Sample, Upload file or Load URL, or press Sample to see an example.
  2. Leave Top-level class name empty to name the root type after the root element, or type your own name.
  3. Tick Types only (no helpers) for structs only, or leave it off to add a package clause and JSON helper functions.
  4. Press Generate (it also updates as you edit), then use Copy or Download.

XML to Go structs

Go's standard library decodes XML with encoding/xml, using struct tags to say whether a field comes from an attribute, a child element or the element's text. This XML to Go generator infers the struct layout and field types from a sample document. It writes json tags, since it models the JSON form of the data, so the one step left is to turn them into xml tags, as shown below.

Example: a server configuration

<server name="api-1">
  <listen port="8080" tls="false"/>
  <route path="/users" method="GET">users.List</route>
  <route path="/users" method="POST">users.Create</route>
  <timeout>30</timeout>
</server>
type Server struct {
	Listen  Listen  `json:"listen"`
	Route   []Route `json:"route"`
	Timeout int64   `json:"timeout"`
	Name    string  `json:"name"`
}

type Listen struct {
	Port int64 `json:"port"`
	TLS  bool  `json:"tls"`
}

type Route struct {
	Value  string `json:"value"`
	Path   string `json:"path"`
	Method string `json:"method"`
}

Converting the tags for encoding/xml

type Server struct {
	XMLName xml.Name `xml:"server"`
	Name    string   `xml:"name,attr"`
	Listen  Listen   `xml:"listen"`
	Route   []Route  `xml:"route"`
	Timeout int64    `xml:"timeout"`
}

type Route struct {
	Path   string `xml:"path,attr"`
	Method string `xml:"method,attr"`
	Value  string `xml:",chardata"`
}
  • ,attr marks an attribute; the generator does not distinguish attributes from elements, so check the original XML for each field.
  • ,chardata maps the element's text, which the generator names Value.
  • A slice with xml:"route" collects repeated <route> children.
  • XMLName checks the root element name; it is optional.

Then decode with xml.Unmarshal(data, &server); encoding/xml converts attribute and element text to int64 and bool for you.

Streaming large XML files

xml.Unmarshal reads the whole document into memory. For large exports, create an xml.NewDecoder(file), loop over decoder.Token(), and when you reach a StartElement named route, call decoder.DecodeElement(&route, &start) to decode just that element into the generated Route struct. Memory use then stays constant no matter how many records the file contains.

Pitfalls

  • Elements that repeat must appear at least twice in the sample to become slices. If the sample mixes one and many, the generator creates a wrapper struct with a pointer and a slice field; simplify it to a slice for XML.
  • Namespace prefixes are removed in field names. encoding/xml matches on local names by default, so tags usually work without them.
  • Leading zeros in numeric-looking text are lost when typed int64; use string for codes.

For JSON APIs, JSON to Go generates structs with ready-to-use json tags. Inspect the XML with the XML Viewer or tidy it with the XML Formatter. Generation runs in your browser.

Frequently asked questions

Why does the XML to Go output have json tags?
The generator models the JSON form of your XML. Replace the tags with xml tags, adding ,attr for attributes and ,chardata for element text, to use encoding/xml.
How do I map an XML attribute in Go?
Use a struct tag like xml:"port,attr". Without ,attr, encoding/xml looks for a child element with that name.
How do I read the text of an element that also has attributes?
Add a field tagged xml:",chardata". The generator names that field Value.
Does encoding/xml convert numbers and booleans?
Yes. Fields typed int64, float64 or bool are parsed from the attribute or element text automatically.

Last updated September 26, 2026

Related tools

JSON to Go Generate Go structs with json tags from a JSON sample instantly, with nested types detected automatically. XML to Haskell Generate Haskell records with Aeson instances from a XML sample instantly, with nested types detected automatically. XML to Java Generate Java POJO classes with getters and setters from a XML sample instantly, with nested types detected automatically. XML to JavaScript PropTypes Generate JavaScript PropTypes React PropTypes definitions from a XML sample instantly, with nested types detected automatically. XML to JSON Schema Generate JSON Schema a JSON Schema document from a XML sample instantly, with nested types detected automatically. XML to Kotlin Generate Kotlin data classes from a XML sample instantly, with nested types detected automatically. XML to Objective-C Generate Objective-C classes with properties from a XML sample instantly, with nested types detected automatically. XML to PHP Generate PHP classes with typed properties from a XML sample instantly, with nested types detected automatically. XML to Pike Generate Pike classes from a XML sample instantly, with nested types detected automatically. XML to Python Generate Python dataclasses with type hints from a XML sample instantly, with nested types detected automatically. XML to Ruby Generate Ruby Dry::Struct classes from a XML sample instantly, with nested types detected automatically. XML to Rust Generate Rust structs with Serde derives from a XML sample instantly, with nested types detected automatically.