13

recently I am planing to do a project using Go. My problem is : given a XSD file, how to auto-generate structs in Go to represent the XML elements. It is similar to using 'xjc' to generate java classes from XSD. So is there any tools or packages to do this in Go?

Thanks in advance.

user2912044
  • 131
  • 1
  • 1
  • 3
  • 1
    see https://github.com/csimplestring/xsd-2-go I just make a tool by myself, this tool can generated Golang structs with XML tags from xsd file. – user2912044 Jun 27 '14 at 09:15

2 Answers2

10

Looks like it might need some TLC for 1.2 support, but there's: https://github.com/metaleap/go-xsd

Which has a set of example packages that have been generated for different common XML formats: https://github.com/metaleap/go-xsd-pkg

Eve Freeman
  • 32,467
  • 4
  • 86
  • 101
  • 1
    go-xsd is abandoned since 2013, and no longer compiles. See for example [issue #37](https://github.com/metaleap/go-xsd/issues/37). – rustyx Jan 13 '21 at 12:23
5

I started writing github.com/ivarg/goxsd for generating XML structs from an XSD spec. While it is still incomplete in relation to the XSD schema reference, It does cover for some of the most common use cases. Maybe it can be useful for others.

It is currently written to be run from command line, or as a go generate statement within a source file. It produces formatted code, and provides options to:

  • generate exported or unexported XML structs
  • name an output file (default is stdout)
  • specify a package name

It currently only handles XSD shema files in the local file system, and does not handle namespaces (they are ignored), or generate validation code.

ivarg
  • 755
  • 1
  • 8
  • 11