XML to Go Converter

Shape XML into Go structs with xml.Name scaffolding, optional JSON tags, and omitempty flags you control. Nothing uploads. The preview is a starting point for hand-tuned types.

  • 0 server round trips for the conversion pass
  • Local DOMParser plus a small struct walk
  • Typical use: legacy SOAP-ish payloads or config snippets

Ingress

XML

Emit

.go

Stop treating generated structs as the final word

The hard part of XML in Go is not typing struct keywords. The hard part is knowing which nodes repeat, which attributes matter, and when a string is really a decimal or a timestamp. This page gives you a fast sketch from a sample document so you are not staring at a blank file.

We walk the first document you paste, group repeated child tags into slices, and attach xml (and optional json) tags so encoding/xml has something concrete to bind. Numbers are guessed from text shape, not from XSD rules. Attributes become separate fields with ,attr in the xml tag when xml tags stay enabled.

Three facts people assume wrong about XML → struct tools

From paste to package: what the pipeline does

Your XML loads through DOMParser. If the browser reports a parse error, we stop and show the left banner so you fix entities or closing tags first. When the tree is valid, we start at the root element, read direct child elements and attributes, and build nested struct types for distinct element names.

Each generated type carries XMLName xml.Name with a root tag string so unmarshaling has an anchor. We also emit a stub UnmarshalXML method you extend or delete. The stub is honest about being a placeholder: many teams prefer standard Unmarshal with struct tags alone.

Privacy. Parsing and code generation run in the tab. Clear both panes before you leave a shared machine if the XML is sensitive.

Where this fits next to JSON workflows

JSON-first services still receive XML at edges: bank files, government feeds, older ERP exports. When you already normalized JSON for the rest of the stack, compare the same payload through our XML to JSON converter to see field names side by side before you commit to Go struct names.

SOAP payloads and three-field configs

Config stubs
Small trees with predictable keys are ideal. Tighten string versus numeric types before you commit them to production structs.
SOAP envelopes
Large envelopes with repeating Body children often need slice fields and renamed types. Expect to split nested types into separate files by hand.
Hand-authored snippets
Quick tests from Postman or curl responses work well. Validate shape first with the XML validator if the source is noisy.
Python or C# shops
If Go is one stop in a polyglot pipeline, pair output review with XML to Python or the C-family generators so naming stays aligned across services.

Regex versus this page: when each wins

Regex stays tempting for one-off extractions. The win here is structural: you get nested types instead of a flat map of strings, which matters once you call xml.Unmarshal for real. Regex still wins for log scraping or when you only need one XPath-like value and refuse to model the whole tree.

ApproachStrengthWeak spot
This generatorFast struct skeleton from a sampleNo XSD or RELAX NG awareness
Official codegen from schemaExact models when schema is trustworthyHeavier setup, schema must exist
Manual structsFull control over tags and pointersSlow for wide documents

Rough edges you should expect

CDATA blocks collapse to text. Comments disappear. Processing instructions are ignored. Order of sibling elements with different names is preserved only indirectly through separate fields, not through a capture-everything slice. If you need ordered mixed lists, generate once, then refactor into custom types.

Omitempty adds ,omitempty to tags you toggle on. Empty semantics still follow Go rules: zero values skip during marshal when omitempty is present, which surprises newcomers for structs and pointers.

Alternative routes when Go is not the bottleneck

Sometimes the task is exploration, not compilation. Pretty-print the same file with the XML pretty print tool, test expressions in the XPath tester, or diff variants with the XML diff utility before you lock struct names.

After you paste: a sane review order

Treat the first output as a sketch, not a merge request.

  1. Run gofmt, then rename types that collide with stdlib names or internal packages.
  2. Replace guessed numeric types where you know the schema: counts want int, money often wants a decimal type or string.
  3. Decide which fields should be pointers so absent XML nodes distinguish nil from empty strings.
  4. Delete or implement the stub UnmarshalXML method based on whether you need custom start-element handling.
  5. Marshal a round trip sample with xml.Marshal and compare against the source document for surprises.

Teams working near SOAP stacks often keep a golden-file test: one trimmed response checked into the repo, unmarshaled in CI, compared field by field. The generator shortens day-one typing. Tests keep day-fifty honest.

Last reviewed: March 2026. Generator output is a draft. Run gofmt, adjust pointer versus value fields, and add tests against real payloads.