如何从 xsd 模式生成 simple-xml java 注释对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6193372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate simple-xml java annotated object from xsd schema
提问by chemical
Ok, so I've searched stackoverflow and found unanswered sub-topics regarding this issue, that's why I'd like to have this question clearly pointed out:
好的,所以我搜索了 stackoverflow 并找到了有关此问题的未回答的子主题,这就是为什么我想明确指出这个问题:
Is there any tool that will generate the Simple Xml library's annotated java class from an xsd schema?
是否有任何工具可以从 xsd 模式生成Simple Xml 库的带注释的 java 类?
I received a couple of xsd files describing the objects returned by a restful web-service someone else has developed and currently I've translated those schemas to the simple-xml annotated classes. Those will be used in my Android app. It would be better to just automatically synchronize any changes to the schemas and regenerate the classes from them. The ws guys have a repository of Hymanson compliant classes for those schemas, however I don't want to use Hymanson - I'm using Spring Android, so I'd prefer to go with the preferred simple-xml. There must be something that does the trick like JAXB's xjc tool.
我收到了几个 xsd 文件,这些文件描述了其他人开发的宁静 Web 服务返回的对象,目前我已将这些模式转换为 simple-xml 带注释的类。这些将用于我的 Android 应用程序。最好是自动同步对模式的任何更改并从中重新生成类。ws 人员有一个用于这些模式的符合 Hymanson 的类的存储库,但是我不想使用 Hymanson - 我使用的是 Spring Android,所以我更愿意使用首选的simple-xml。必须有一些东西可以像 JAXB 的 xjc 工具那样做。
If there is no such tool - can you think of any traps in implementing a script that generates a .java file for simple-xmlfrom the schema? Maybe any hints for tools worth extending that would just require defining what annotations to generate and when?
如果没有这样的工具 - 您能想到在实现从模式生成simple-xml的 .java 文件的脚本时有什么陷阱吗?也许任何值得扩展的工具提示只需要定义要生成的注释以及何时生成?
In advance - thanks a lot for your answers!
提前 - 非常感谢您的回答!
采纳答案by Robert Massaioli
I do not believe that JAXBcan be used on Android because of missing package requirements (see here), especially with earlier versions of the Android API. What you could do instead is send your XSD through xjc and get JAXB class output and then write a script to convert the JAXB annotations into the equivalent Simple XMLannotations. This should do exactly what you were looking for.
我不相信JAXB可以在 Android 上使用,因为缺少包要求(请参阅此处),尤其是对于早期版本的 Android API。您可以做的是通过 xjc 发送您的 XSD 并获得 JAXB 类输出,然后编写一个脚本将 JAXB 注释转换为等效的简单 XML注释。这应该完全符合您的要求。
However, ideally, and if you had the time, you could check out the source for xjc and extend it to be able to output JAXB or Simple annotated classes.
但是,理想情况下,如果您有时间,可以查看 xjc 的源代码并将其扩展为能够输出 JAXB 或 Simple 带注释的类。
回答by Yeshodhan Kulkarni
I wrote a library to generate SimpleXML Java annotated classes from XSD.
我编写了一个库来从 XSD 生成 SimpleXML Java 注释类。
Here is the link: https://github.com/yeshodhan/android-jaxb
这是链接:https: //github.com/yeshodhan/android-jaxb
Eg:
例如:
XML Schema
XML 架构
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://person.mickoo.com/"
targetNamespace="http://person.mickoo.com/" elementFormDefault="qualified">
<xsd:element name="Person" type="Person"/>
<xsd:complexType name="Person">
<xsd:sequence>
<xsd:element name="FirstName" type="xsd:string" minOccurs="0" />
<xsd:element name="LastName" type="xsd:string" minOccurs="0" />
<xsd:element name="Adult" type="xsd:boolean" minOccurs="0" />
<xsd:element name="Addresses" type="Addresses" minOccurs="0" />
<xsd:element name="Gender" type="Gender" minOccurs="0" />
<xsd:element name="Favorite_Fruits" type="Fruits" minOccurs="0" maxOccurs="3"/>
<xsd:element name="SomeThing_really_whacky-by-the-user" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="Addresses">
<xsd:sequence>
<xsd:element name="Address" type="Address" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Address">
<xsd:sequence>
<xsd:element name="Line1" type="xsd:string" minOccurs="0" />
<xsd:element name="Line2" type="xsd:string" minOccurs="0" />
<xsd:element name="City" type="xsd:string" minOccurs="0" />
<xsd:element name="State" type="xsd:string" minOccurs="0" />
<xsd:element name="Country" type="xsd:string" minOccurs="1" />
<xsd:element name="PostalCode" type="xsd:string" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="Gender">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="MALE"/>
<xsd:enumeration value="FEMALE"/>
<xsd:enumeration value="NOT_SPECIFIED"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="Fruits">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Apple"/>
<xsd:enumeration value="Banana"/>
<xsd:enumeration value="Mango"/>
<xsd:enumeration value="Orange"/>
<xsd:enumeration value="Grapes"/>
<xsd:enumeration value="Watermelon"/>
<xsd:enumeration value="Peach"/>
<xsd:enumeration value="Apricot"/>
<xsd:enumeration value="Grapefruit"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Generated Java Classes
生成的 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> address;
public Addresses() {
}
public List<Address> getAddress() {
return address;
}
public void setAddress(List<Address> 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<Fruits> 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<Fruits> getFavoriteFruits() {
return favoriteFruits;
}
public void setFavoriteFruits(List<Fruits> 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;
}
}
Usage
用法
? 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 <arg> (optional) bindings JSON file
-d,--destination <arg> destination directory for generated classes
-h,--help Help on usage
-p,--package <arg> package name for generated classes. Eg.:
com.example.app
-v,--version Version
---------------------------------------------------------------------
回答by Cratylus
As far as I know, Simple XML is used to create annotated classes to marshall/demarshall XML.
The Java Web Services frameworks, use specific bindings:
E.g. CXF supports (besides JAXB) Aegis, XML Beans etc CXF data bindings
Axis2 supports ADB, XMLBeans etc Axis2 data bindings
Same for JAX-WS
I do not know about Spring WS (but I doubt they support specifically simple)
IMHO, if you need absolutely to use Simple XML, just create the classes yourself. This seems feasible in your case since you note:
据我所知,简单 XML 用于创建带注释的类以编组/解组 XML。
Java Web 服务框架,使用特定绑定:
例如 CXF 支持(除了 JAXB)Aegis、XML Beans 等CXF 数据绑定
Axis2 支持 ADB、XMLBeans 等Axis2 数据绑定
JAX-WS 同样
我不知道 Spring WS(但我怀疑他们支持特别简单)
恕我直言,如果您绝对需要使用简单的 XML,只需自己创建类。这在您的情况下似乎可行,因为您注意到:
I received a couple of xsd files describing the objects returned by a restful web-service
So for a few files may be it is possible. I do not think you should worry about changes in the schema by the web service implementers, since if they do changes you would have to regenerate your client part anyhow. In any case once a web service is deployed the schemas do not change that often otherwise all the client apps would be affected.
If you want to create your own tool, then you would have to create a code generator that would parse the xsd files and create annotated classes for Simple XML. This seems to me a lot of effort
Hope this helps
所以对于几个文件可能是可能的。我不认为您应该担心 Web 服务实现者对架构的更改,因为如果他们进行更改,您无论如何都必须重新生成客户端部分。在任何情况下,一旦部署了 Web 服务,架构就不会更改,否则所有客户端应用程序都会受到影响。
如果您想创建自己的工具,则必须创建一个代码生成器来解析 xsd 文件并为 Simple XML 创建带注释的类。在我看来这需要很多努力
希望这会有所帮助