Generate Go structs from multiple XML documents.
MIT License
Generate Go structs from multiple XML documents.
go-xmlstruct generates Go structs from XML documents. Alternatively put, go-xmlstruct infers XML schemas from one or more example XML documents. For example, given this XML document, go-xmlstruct generates this Go source code.
Compared to existing Go struct generators like zek, XMLGen, and chidley, go-xmlstruct:
bool
, int
, string
, or time.Time
asgo-xmlstruct is useful for quick-and-dirty unmarshalling of arbitrary XML documents, especially when you have no schema or the schema is extremely complex and you want something that "just works" with the documents you have.
Install the goxmlstruct
CLI with:
$ go install github.com/twpayne/go-xmlstruct/cmd/goxmlstruct@latest
Feed goxmlstruct
the simple XML document:
<parent>
<child flag="true">
text
</child>
</parent>
by running:
$ echo '<parent><child flag="true">text</child></parent>' | goxmlstruct
This produces the output:
// Code generated by goxmlstruct. DO NOT EDIT.
package main
type Parent struct {
Child struct {
Flag bool `xml:"flag,attr"`
CharData string `xml:",chardata"`
} `xml:"child"`
}
This demonstrates:
For a full list of options to the goxmlstruct
CLI run:
$ goxmlstruct -help
You can run a more advanced example with:
$ git clone https://github.com/twpayne/go-xmlstruct.git
$ cd go-xmlstruct
$ goxmlstruct internal/tests/gpx/testdata/*.gpx
This demonstrates generating a Go struct from multiple complex XML documents.
For an example of configurable field naming and named types by using
go-xmlstruct as a package, see
internal/tests/play/play_test.go
.
For an example of a complex schema, see
internal/tests/aixm/aixm_test.go
.
Similar to go-jsonstruct, go-xmlstruct consists of two phases:
MIT