从XSD生成Go结构体

huangapple go评论81阅读模式
英文:

generate Go structs from XSD

问题

最近我计划使用Go语言做一个项目。我的问题是:给定一个XSD文件,如何自动生成Go语言的结构体来表示XML元素。类似于使用"xjc"从XSD生成Java类。所以在Go语言中有没有类似的工具或包可以实现这个功能?

提前感谢。

英文:

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.

答案1

得分: 10

看起来可能需要一些关注来支持1.2版本,但有以下资源可用:

https://github.com/metaleap/go-xsd

这个库提供了一组为不同常见XML格式生成的示例包。

https://github.com/metaleap/go-xsd-pkg

英文:

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

答案2

得分: 5

我开始编写github.com/ivarg/goxsd,用于从XSD规范生成XML结构。虽然与XSD模式参考相比仍然不完整,但它确实涵盖了一些最常见的用例。也许对其他人有用。

目前它是为了从命令行运行,或作为源文件中的go generate语句而编写的。它生成格式化的代码,并提供以下选项:

  • 生成导出或未导出的XML结构
  • 指定输出文件名(默认为stdout)
  • 指定包名

它目前只处理本地文件系统中的XSD模式文件,并且处理命名空间(它们被忽略),也不生成验证代码。

英文:

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.

huangapple
  • 本文由 发表于 2013年12月23日 05:57:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/20734450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定