Format Code Now
/
ConvertXML to Go Next: XML to Java ›

XML to Go Struct Generator

CV-80

Paste an XML document and press Convert to get structs with json tags. The XML is read into JSON first, so attributes, text and repeated elements follow the rules below, and it runs in your browser.

Runs in your browser. Nothing is uploaded.

XML

Go

How to use it

  1. Paste an XML document, or open an .xml file. Use one that shows every element you expect, with repeating elements repeated.
  2. Choose Go initialisms, pointers for missing keys and whether every field gets omitempty. Leave typed values on to read 59.90 as a number and true as a boolean, or turn it off to keep every value as text.
  3. Press Convert, then copy the result or download it as a .go file.
  4. Unmarshal into the first struct with json.Unmarshal(data, &root). Feed it the JSON form of your XML, not the XML itself.

How the structs are built

Each object becomes a struct with exported CamelCase field names and a json tag holding the original key. A key that is null in some items becomes a pointer, and a key missing from some objects becomes a pointer with omitempty. Common initialisms such as ID, URL and HTTP are written in capitals, which you can switch off. Mixed types, empty arrays and keys that are always null become interface{}, and a key with a comma or backtick in it raises an error because a struct tag cannot hold it.

How the XML becomes structs

An attribute becomes a key that starts with @, and text that sits next to attributes or child elements becomes #text. An element that repeats under the same parent becomes a list, while an element that appears once stays a single value. Text that is exactly a number or true/false is typed as such when the option is on (the default); a leading zero or plus sign keeps it as a string. Comments, processing instructions and the DOCTYPE are dropped, and namespace prefixes stay in the name. In Go, @id becomes the field ID with the key kept in the json tag, and a clash with a child element of the same name gets a number.

What a sample cannot tell it

The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay string; change them to time.Time by hand, and check pointer fields against what your data can really contain. An element that appears once in your sample is written as a single value; if real documents can repeat it, paste a sample where it appears at least twice so it becomes a list.

Questions people ask

How do I convert XML to a Go struct?

Paste the XML into the input box, choose your options and press Convert to get the structs. Unmarshal into the first struct with json.Unmarshal(data, &root). The structs follow the JSON form of the XML. The conversion happens in your browser.

What do the initialisms do in Go field names?

With initialisms on, words such as id, url, http, api and uuid are written in capitals in the field name, so a key user_id becomes UserID, as Go linters expect. The json tag keeps the original key either way. This applies to the JSON form of your XML.

Why does the Go converter complain about a comma or backtick?

A Go struct tag is written between backticks and splits its options at commas, so a key containing either character cannot be stored in the json tag. The error names the key; rename it in the sample, then convert again. This applies to the JSON form of your XML.

Why is a Go field typed interface{}?

Mixed types in one key, a key that is always null, and an empty array all give interface{}, because the sample does not show one clear type. Replace it with the type you expect, or paste a sample where the key has a value. This applies to the JSON form of your XML.

Does it read the XML declaration, comments or namespaces?

The declaration, comments, processing instructions and DOCTYPE are read and then dropped. Namespace prefixes stay as part of the element name, and namespaces are not resolved. Custom entities are an error; only the five built-in ones and numeric character references are decoded. The Go structs are written from that converted data.

Are numbers in XML text typed as numbers?

By default, yes: text that is exactly a number, such as 59.90 or 3, becomes a number, and true or false becomes a boolean. Text with a leading zero or plus sign, like 007, stays a string. Turn the option off to make every value a string. The Go structs are written from that converted data.

About this tool

The XML is read first and turned into the same JSON that the XML to JSON converter writes, and the code is generated from that. The root element becomes the single top-level key, so the root class holds one field named after it. An attribute becomes a key that starts with @, text next to attributes or child elements becomes #text, an element that appears more than once under one parent becomes a list, and one that appears once stays a single value, so a sample with one item gives a single object where another sample would give a list. Comments, processing instructions and the DOCTYPE are dropped. With the typed-values option on (the default), text that is exactly a number or true/false becomes a number or boolean; text with a leading zero or plus sign stays a string, and turning the option off makes every value a string. The result describes the converted JSON, not an XmlSerializer or JAXB mapping. Every object becomes a struct with exported CamelCase fields and a json tag that keeps the original key. Whole numbers become int, or int64 beyond 32 bits; decimals become float64, and an integer beyond int64 also becomes float64 and can round. A key that is null in some items becomes a pointer and a key missing from some objects gets omitempty. Mixed types, always-null keys and empty arrays become interface{}. Keys that contain a comma or a backtick cannot go in a tag and raise an error. Everything runs in your browser; nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.