Loading XML to Go Converter...
Please wait a moment

How to Convert XML to Go Structs - Step by Step Guide

Step 1

Paste or upload your XML

Start by adding your XML data that needs to be converted to Go structs. The tool analyzes the XML structure to generate appropriate structs with XML tags, perfect for SOAP services and RSS parsing.

Example: Try This XML Data

Copy and paste this XML example to see how it works:

<?xml version="1.0" encoding="UTF-8"?>
<server>
  <config id="main">
    <host>localhost</host>
    <port>8080</port>
    <ssl_enabled>true</ssl_enabled>
    <max_connections>100</max_connections>
    <database>
      <driver>postgres</driver>
      <connection_string>postgres://user:pass@localhost/db</connection_string>
    </database>
  </config>
</server>
Step 2

Check the types and tags

There is nothing to set up: the Go structs follow your XML. Every copy of an element is looked at, and the field type is the one that fits all of its values.

•
Whole numbers: int, then int64 past 32 bits and uint64 above the int64 range, so an ID like 8901240544102066246 keeps every digit
•
Decimals and flags: 14.50 becomes float64, and only the exact words true and false become bool
•
Kept as text: 00501, 0x1F, numbers past 64 bits and elements that are sometimes empty stay string
•
Tags: encoding/xml tags with ,attr for attributes and ,chardata for an element's own text
Step 3

Read the generated structs

The example from Step 1 becomes these typed Go structs with proper XML unmarshaling support.

Go Code Output

The output file starts with package main and import "encoding/xml", followed by:

type Root struct {
	XMLName xml.Name `xml:"server"`
	Config  Config   `xml:"config"`
}

type Config struct {
	ID             string   `xml:"id,attr"`
	Host           string   `xml:"host"`
	Port           int      `xml:"port"`
	SSLEnabled     bool     `xml:"ssl_enabled"`
	MaxConnections int      `xml:"max_connections"`
	Database       Database `xml:"database"`
}

type Database struct {
	Driver           string `xml:"driver"`
	ConnectionString string `xml:"connection_string"`
}
Step 4

Copy or download the Go code

Copy the structs into your package or download them as structs.go, then read a document with xml.Unmarshal.

•
Copy to clipboard for immediate use in your Go projects
•
Download .go file for integration into your codebase
•
XML endpoints in Gin and Echo
•
Data processing and XML parsing workflows

What is XML to Go Conversion?

XML to Go conversion transforms XML schemas into Go structs with proper XML tags and type annotations. It reads the structure and values of your document and writes Go code that encoding/xml can unmarshal into typed fields.

The generated structs carry the original XML names in their tags, so the Go names can follow Go conventions while still matching your documents exactly.

Frequently Asked Questions

How do I read my XML into the generated structs?

Put the code in your package (change package main if you need to), then call xml.Unmarshal(data, &root) with a variable of the root struct type. The root struct carries an XMLName field naming the top element, every field has an encoding/xml tag with the original XML name, and only the standard library is needed.

How are the field types picked?

From how the values are written in your XML, across every copy of an element. Whole numbers become int, int64 once they pass 32 bits, or uint64 above the int64 range, so an ID like 8901240544102066246 is read without losing a digit; anything past 64 bits stays a string. Decimals such as 14.50 become float64, only the exact words true and false become bool, and values like 00501, 0x1F or an element that is sometimes empty stay string, because Go would otherwise drop the zeros, fail on the hex or quietly read an empty value as 0.

What happens with attributes, text and repeated elements?

Attributes get a ,attr tag and an element's own text goes into a Value field tagged ,chardata, so <price currency="USD">10.50</price> keeps both. Every copy of an element is looked at, not just the first, so a field that only appears in the second <item> still ends up in the struct. An element that repeats under one parent becomes a slice, nested elements get their own struct, and an element that contains itself is held through a pointer so the code compiles.

How are XML names turned into Go names?

Field and type names have to be exported or encoding/xml ignores them, so ssl_enabled becomes SSLEnabled and client-id becomes ClientID, using Go's usual capitalised initialisms such as ID, URL and HTTP. Names that start with a digit or with a character that has no capital form get an X in front, and two names that end up the same get a number. The tag always keeps the exact XML name, including the namespace URI for prefixed elements such as ns:item.

Can I use the structs with Gin or Echo?

Yes, as long as the request or response is XML. Gin's ShouldBindXML and c.XML, and Echo's XML binding, all go through encoding/xml, so the same tags apply. If your API speaks JSON instead, add json tags yourself or start from the JSON to Go converter.

Is my XML uploaded anywhere?

No. The conversion runs in your browser, and the XML you paste or upload is not sent to our server. Very large documents are limited only by your browser's memory and may take a moment to process.