XML to Go struct
Paste an XML sample and get Go structs with xml tags: attributes, child elements and chardata, with exported field names, slices for repeated elements and omitempty; invalid XML is reported with line and column.
Runs in your browserEvery computation happens in your browser — your data never leaves this device.
When off, fields are unexported and invisible outside the package
Integers, floats and true/false are inferred from the content, everything else becomes string; a ,chardata field is only added when the element also has attributes or child elements
What this tool does
- Wiring up a SOAP or third-party XML API? Paste a real response and get a Go struct you can Unmarshal straight away instead of hand-writing struct tags.
- Document an interface by turning sample payloads into struct definitions with xml tags, field names, types and attribute markers filled in.
- Repeated elements become slices ([]string) automatically, and integers, floats and true/false are inferred from the content.
- Fine-tune the result when needed: turn off exported fields, drop ,attr / ,chardata, add omitempty, or set the package name to match your project.
Example
Input
<user id="42"><name>Ada</name><tag>go</tag><tag>xml</tag></user>
Output
package main
import "encoding/xml"
type User struct {
XMLName xml.Name `xml:"user"`
ID int `xml:"id,attr"`
Name string `xml:"name"`
Tag []string `xml:"tag"`
}Output with the default options (package main, exported fields, ,attr fields, slices for repeated elements). The root struct gains an XMLName field, and the attribute value "42" is inferred as int.
Frequently asked questions
How are field types inferred?
From the element text: all-integer content (optionally negative) becomes int, values like 1.5 become float64, all true/false becomes bool, and everything else — including empty or mixed content — stays string. Inference only sees the sample, so if real data mixes formats Unmarshal fails; change the field to string when in doubt.
When does a nested element get its own struct?
When a child has element children of its own, or has attributes while the ,attr option is on, it gets a dedicated struct and the parent field uses that type. Child elements holding only text are merged into a slice instead, so <tag>go</tag><tag>xml</tag> becomes Tag []string.
What about elements with both text and child elements?
If an element has attributes and direct text, an extra Value field tagged `xml:",chardata"` is generated to hold that text. Elements that contain only text — no attributes, no children — do not get a chardata field.
How are names converted?
Element and attribute names are converted to PascalCase (user_id → UserID, id → ID, url → URL, since common initialisms stay upper case), with dashes, underscores and dots treated as separators. A leading digit gets an underscore prefix. Turning off "Export field names" lowercases the fields so they stay package-private.
What happens when the XML is malformed?
You get an error with a position, for example invalid-xml:line 3 column 5:mismatched-tag, which the UI renders as "Line 3, column 5: A closing tag does not match its opening tag". Comments, CDATA, processing instructions and DOCTYPE declarations are skipped by the parser and do not affect the structure. Everything runs locally in your browser.
Keywords:xmlgogolangstructtagunmarshalxml tag代码生成结构体转换