$Id: 334
Writing struct definitions that match XML file is tedious.
There are tools that can generate that definition based on sample XML file.
A downside of such approach is that if sample XML doesn't have all the fields, they won't be generated.
XML To Go online tool allows to do it in a web browser. Paste XML sample on the left, get Go struct definition on the right.
Zek is a Go program that reads XML from a file or stdin and prints Go struct definition to stdout.
You can use chidley to automatically generate struct definitions from sample XML file.
Install the tool with go get -u github.com/gnewton/chidley
.
chidley sample.xml
will print Go struct definitions deduced from sample.xml
to standard out.
Run chidley
to list all cmd-line options.
Options that I find useful:
-X
: sort generated structs by order in XML file (default is alphabetically)-a ""
: prefix for attribute names (default is Attr
)-e ""
: prefix for struct names (default is C
i.e. by default struct name for XML element Api
would CApi
; this makes it just Api
)-t
: by default all fields are strings. This flag tries to infer type from valuesAll options: