XML to Go

Generate Go types from XML automatically.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What XML to Go does

XML to Go turns a sample XML document into Go code: one struct with json tags per object, with a type for every field inferred from the value in the sample.

XML to Go examples

The built-in sample

Input

<users><user id="1"><name>Ada</name></user><user id="2"><name>Lin</name></user></users>

Output

package main

type Root struct {
	Id string `json:"@id"`
	Name string `json:"name"`
}

This is the input that the Load sample button fills in.

Attributes, repeated elements and empty elements

Input

<order id="7" paid="false"><total>19.5</total><item sku="A1"><qty>2</qty></item><item sku="B2"><qty>1</qty><gift/></item><note/></order>

Output

package main

type Item struct {
	Sku string `json:"@sku"`
	Qty string `json:"qty"`
	Gift string `json:"gift"`
}

type Root struct {
	Id string `json:"@id"`
	Paid string `json:"@paid"`
	Total string `json:"total"`
	Item []Item `json:"item"`
	Note string `json:"note"`
}

In the sample, id, paid and sku are attributes, item repeats, and gift and note are empty elements. Every value is typed as a string, because XML does not carry types.

Each output was produced by running this tool on the input above it.

How it works

The sample is walked recursively and each value is mapped to a Go type:

Sample valueGo type
Stringstring
Whole numberint
Decimal numberfloat64
true / falsebool
null, or an empty array’s itemsinterface{}
Array of T[]T
Objecta new named type

XML has no data types, so every leaf value is generated as the Go string type. Attributes become fields too: the attribute name is capitalised like any other field (Id). Repeated elements become lists, and the root element itself is always called Root.

Reading XML into the generated types

The generated tags are json tags. For encoding/xml, change them to xml:"id,attr" for attributes and xml:"total" for child elements.

Details and limits

  • Field names are made exportable by capitalising each word (order-id becomes OrderId). A key that starts with a digit keeps the digit and is not a valid Go identifier until you rename it.
  • The output starts with package main; change it to your package name.
  • The fields are not column-aligned. Run gofmt on the file.
  • A null value becomes interface{}. Use a pointer type such as *string once you know the real type.
  • A type is named after the key that holds the object: items gives a type called Items, with no attempt to make it singular. The top-level type is always Root.
  • If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
  • Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
  • Input that is not valid XML stops the tool, and the status line shows where the problem is. The XML Validator page lists the usual causes.

Questions

Is my data uploaded when I use XML to Go?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does XML to Go do?

XML to Go turns a sample XML document into Go code: one struct with json tags per object, with a type for every field inferred from the value in the sample.

Are there any limits I should know about?

Field names are made exportable by capitalising each word (order-id becomes OrderId). A key that starts with a digit keeps the digit and is not a valid Go identifier until you rename it. The output starts with package main; change it to your package name.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.