如何从xsd模式生成simple-xml java注释对象

好的,所以我搜索了stackoverflow并find了关于这个问题的未回答的子主题,这就是为什么我想明确指出这个问题:

是否有任何工具可以从xsd模式生成Simple Xml库的带注释的java类?

我收到了几个xsd文件,描述了其他人开发的一个安静的Web服务返回的对象,目前我已经将这些模式转换为simple-xml带注释的类。 这些将用于我的Android应用程序。 最好是自动同步对模式的任何更改并从中重新生成类。 ws们有一个针对这些模式的Jackson兼容类的存储库,但是我不想使用Jackson – 我使用的是Spring Android,所以我更喜欢使用首选的simple-xml 。 必须有像JAXB的xjc工具那样的技巧。

如果没有这样的工具 – 你能想到实现一个脚本,从模式中为simple-xml生成一个.java文件吗? 也许任何值得扩展的工具的提示只需要定义要生成的注释和何时?

提前 – 非常感谢您的回答!

我不相信JAXB可以在Android上使用,因为缺少包的要求( 见这里 ),尤其是早期版本的Android API。 您可以做的是通过xjc发送XSD并获取JAXB类输出,然后编写脚本以将JAXB注释转换为等效的Simple XML注释。 这应该完全符合您的要求。

但是,理想情况下,如果您有时间,可以查看xjc的源代码并将其扩展为能够输出JAXB或Simple注释类。

我编写了一个库来从XSD生成SimpleXML Java注释类。

这是链接: https : //github.com/yeshodhan/android-jaxb

例如:

XML Schema

< ?xml version="1.0" encoding="UTF-8"?>                                                   

生成的Java类

 package com.mickoo.person; import org.simpleframework.xml.Element; import org.simpleframework.xml.Namespace; import org.simpleframework.xml.Root; @Root(name = "Address") @Namespace(reference = "http://person.mickoo.com/") public class Address { @Element(name = "Line1", required = false) private String line1; @Element(name = "Line2", required = false) private String line2; @Element(name = "City", required = false) private String city; @Element(name = "State", required = false) private String state; @Element(name = "Country", required = true) private String country; @Element(name = "PostalCode", required = false) private String postalCode; public Address() { } public String getLine1() { return line1; } public void setLine1(String line1) { this.line1 = line1; } public String getLine2() { return line2; } public void setLine2(String line2) { this.line2 = line2; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getPostalCode() { return postalCode; } public void setPostalCode(String postalCode) { this.postalCode = postalCode; } } package com.mickoo.person; import java.util.List; import org.simpleframework.xml.ElementList; import org.simpleframework.xml.Namespace; import org.simpleframework.xml.Root; @Root(name = "Addresses") @Namespace(reference = "http://person.mickoo.com/") public class Addresses { @ElementList(name = "Address", entry = "Address", inline = true, required = false) private List
address; public Addresses() { } public List
getAddress() { return address; } public void setAddress(List
address) { this.address = address; } } package com.mickoo.person; import org.simpleframework.xml.Namespace; import org.simpleframework.xml.Root; @Root(name = "Fruits") @Namespace(reference = "http://person.mickoo.com/") public enum Fruits { Apple, Banana, Mango, Orange, Grapes, Watermelon, Peach, Apricot, Grapefruit; } package com.mickoo.person; import org.simpleframework.xml.Namespace; import org.simpleframework.xml.Root; @Root(name = "Gender") @Namespace(reference = "http://person.mickoo.com/") public enum GenderEnum { MALE(0, "Men are from Mars"), FEMALE(1, "Women are from Venus"), NOT_SPECIFIED(2, "Can't say anything"); private final Integer id; private final String description; private GenderEnum(Integer id, String description) { this.id = id; this.description = description; } public Integer id() { return id; } public String description() { return description; } } package com.mickoo.person; import java.util.List; import org.simpleframework.xml.Attribute; import org.simpleframework.xml.Element; import org.simpleframework.xml.ElementList; import org.simpleframework.xml.Namespace; import org.simpleframework.xml.Root; @Root(name = "Person") @Namespace(reference = "http://person.mickoo.com/") public class Person { @Element(name = "FirstName", required = false) private String firstName; @Element(name = "LastName", required = false) private String lastName; @Element(name = "Adult", required = false) private Boolean adult; @Element(name = "Addresses", required = false) private Addresses addresses; @Element(name = "Gender", required = false) private GenderEnum gender; @ElementList(name = "Favorite_Fruits", entry = "Favorite_Fruits", inline = true, required = false) private List favoriteFruits; @Element(name = "SomeThing_really_whacky-by-the-user", required = false) private String someThingReallyWhackyByTheUser; @Attribute(name = "id", required = false) private String id; public Person() { } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Boolean getAdult() { return adult; } public void setAdult(Boolean adult) { this.adult = adult; } public Addresses getAddresses() { return addresses; } public void setAddresses(Addresses addresses) { this.addresses = addresses; } public GenderEnum getGender() { return gender; } public void setGender(GenderEnum gender) { this.gender = gender; } public List getFavoriteFruits() { return favoriteFruits; } public void setFavoriteFruits(List favoriteFruits) { this.favoriteFruits = favoriteFruits; } public String getSomeThingReallyWhackyByTheUser() { return someThingReallyWhackyByTheUser; } public void setSomeThingReallyWhackyByTheUser(String someThingReallyWhackyByTheUser) { this.someThingReallyWhackyByTheUser = someThingReallyWhackyByTheUser; } public String getId() { return id; } public void setId(String id) { this.id = id; } }

用法

 ➜ target git:(master) ✗ java -jar android-jaxb-1.0.jar --help usage: java -jar android-jaxb-1.0.jar [options] your-schema-file.xsd --------------------------------------------------------------------- -b,--bindings  (optional) bindings JSON file -d,--destination  destination directory for generated classes -h,--help Help on usage -p,--package  package name for generated classes. Eg.: com.example.app -v,--version Version --------------------------------------------------------------------- 

据我所知,Simple XML用于创建带注释的类来编组/解组XML。
Java Web Services框架使用特定绑定:
例如,CXF支持(除了JAXB)Aegis,XML Beans等CXF数据绑定
Axis2支持ADB,XMLBeans等Axis2数据绑定
JAX-WS也是如此
我不知道Spring WS(但我怀疑他们支持特别简单)
恕我直言,如果您完全需要使用Simple XML,只需自己创建类。 在您注意到这种情况下,这似乎是可行的:

 I received a couple of xsd files describing the objects returned by a restful web-service 

因此对于一些文件可能是可能的。 我不认为您应该担心Web服务实现者对架构的更改,因为如果他们进行了更改,您无论如何都必须重新生成客户端部件。 在任何情况下,一旦部署了Web服务,模式就不会改变,否则所有客户端应用程序都会受到影响。
如果要创建自己的工具,则必须创建一个代码生成器,该代码生成器将解析xsd文件并为Simple XML创建带注释的类。 这在我看来付出了很多努力
希望这可以帮助