XML to Go

Generate Go structs with encoding/xml tags from an XML sample.

XML to Go – Structs for encoding/xml

The XML to Go converter generates Go structs that unmarshal your XML with the standard encoding/xml package — no third-party library needed. Paste a document and you get one struct per element type, complete with struct tags and an XMLName field on the root.

How XML maps to types

XML has no native types, so the converter reads every element and attribute value and infers one: whole numbers become integers, decimals become floating-point numbers, true/false become booleans, and everything else stays a string. Attributes are kept apart from child elements (they appear as @name keys internally), an element that has both attributes and text gets a dedicated text field, and repeated sibling elements such as several item nodes become a list. Namespace prefixes are stripped from names so ns:customer and customer produce the same type.

Struct tags that match encoding/xml

  • Child elements use xml:"name", and attributes use xml:"name,attr".
  • An element that carries text plus attributes, such as <price currency="EUR">9.99</price>, gets a Text field tagged xml:",chardata".
  • Repeated elements become slices, for example Item []Item tagged xml:"item".
  • The root struct starts with XMLName xml.Name so xml.Unmarshal checks the element name.
  • Optional elements get pointers and ,omitempty, which keeps xml.Marshal output clean.

Switch the Struct tags option to json + xml if the same struct is also returned from a JSON API, or to json if you only need JSON tags.

Example use cases

Parsing RSS feeds, bank statement exports, SOAP responses, Maven POM files, or the XML configuration of a legacy system you are porting to Go. Generating the structs avoids the classic bug of a missing ,attr suffix that silently leaves a field empty.

Tips

Names are converted to idiomatic Go with initialisms (ID, URL), so the tag always carries the exact XML name. Use the package option to match your module layout.

Format the input with the XML Formatter or try JSON to Go for JSON payloads.

Frequently Asked Questions

No. The XML is parsed and converted by JavaScript running in your browser tab. Nothing is sent to a server, so internal feeds, SOAP payloads and configuration files stay on your machine.

Elements that are missing from some repeated siblings are optional. With the pointers option on they become *T, so you can tell an absent element from a zero value.

Yes. A wrapper such as <items> becomes its own struct holding a slice of the repeated child. You can collapse it later with a path tag like xml:"items>item" if you prefer.

Yes. Set Struct tags to "json + xml" and every field gets both tags, which is handy when an XML import is exposed through a JSON API.