JAXB:如何在XSD中指定attr类型时更改XJC生成的类名? [英] JAXB: How to change XJC-generated classes names when attr type is specified in XSD?
问题描述
我是JAXB的初学者,在使用xjc生成Java类时遇到了烦人的问题。我提供了这样的XSD:
I'm a beginner to JAXB and I'm having annoying issues when generating Java classes with xjc. I am provided with a XSD like this:
<xs:element name="item" type="itemType"/>
...
<xs:complexType name="itemType">
<xs:attribute name="id" type="xs:string" use="required">
...
</xs:complexType>
并且xjc正在生成一个名为的类ItemType.java
,但我希望名称是 Item.java
。也就是说,我希望生成的类好像XSD是这样的:
and xjc is generating a class called ItemType.java
, but I want the name to be Item.java
. That is, I want the generated classes as if the XSD was like this:
<xs:element name="item">
<xs:complexType>
<xs:attribute name="id" type="xs:string" use="required">
...
</xs:complexType>
</xs:element>
任何其他元素都不会重复使用itemType,只是构建了XSD喜欢这种方式。
我想可能有一种方法可以使用自定义绑定,但我仍然没有找到方法。
There won't be any reuse of itemType on any other element, it's just the people that constructs the XSD likes it this way. I guess there may be a way to do it with custom bindings but I still haven't found how.
任何帮助?
谢谢,
Miguel
Thanks, Miguel
推荐答案
JAXB提供了两种方法来实现这一目标:
JAXB provides two ways to accomplish this:
1。内联架构Anntotations
您可以使用JAXB架构注释来控制类名。
You can use JAXB schema annotations to control the class names.
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1">
<xs:complexType name="itemType">
<xs:annotation>
<xs:appinfo>
<jaxb:class name="Item"/>
</xs:appinfo>
</xs:annotation>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
2。外部绑定文件
此自定义也可以通过外部绑定文件完成:
This customization can also be done via and external binding file:
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="your-schema.xsd">
<jxb:bindings node="//xs:complexType[@name='itemType']">
<jxb:class name="Item"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
xjc命令行将是:
xjc -d out -b binding.xml your-schema.xsd
这篇关于JAXB:如何在XSD中指定attr类型时更改XJC生成的类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!