从xsd创建一个静态变量

时间:2015-01-15 20:05:00

标签: java jaxb xsd

在我正在进行的项目中,我们从多个xsd生成类。我的一些课程需要public static final String EXTENSION_CODE = "NAMEOFTHEEXTENSION";。我班级的xsd看起来像:

<xsd:complexType name="SomeExtension">
    <xsd:sequence>
        <xsd:element name="ID" type="xsd:string"  minOccurs="1" maxOccurs="1"/>
        <xsd:element name="element1" type="Class" minOccurs="0" maxOccurs="1" />
        <xsd:element name="element2" type="Class" minOccurs="0" maxOccurs="1" />
        <xsd:element name="element3" type="Class" minOccurs="0" maxOccurs="1" />
        <xsd:element name="element4" type="Class" minOccurs="0" maxOccurs="1" />
        <xsd:element name="otherID" type="xsd:string"  minOccurs="1" maxOccurs="1"/>
        <xsd:element name="element5" type="xsd:string"  minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
</xsd:complexType>

这将生成一个类似于以下的类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SomeExtension", propOrder = {
    "ID",
    "element1",
    "element2",
    "element3",
    "element4",
    "otherID",
    "element5"
})
public class SomeExtension
    implements Serializable, Cloneable, CopyTo
{

    private final static long serialVersionUID = 1L;
    @XmlElement(required = true)
    protected String iD;
    protected Element1 element1;
    protected Element2 element2;
    protected Element3 element3;
    protected Element4 element4;
    @XmlElement(required = true)
    protected String otherID;
    protected String element5;

我需要的是:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SomeExtension", propOrder = {
    "ID",
    "element1",
    "element2",
    "element3",
    "element4",
    "otherID",
    "element5"
})
public class SomeExtension
    implements Serializable, Cloneable, CopyTo
{

    public static final String EXTENSION_CODE = "NAMEOFTHEEXTENSION";
    private final static long serialVersionUID = 1L;
    @XmlElement(required = true)
    protected String iD;
    protected Element1 element1;
    protected Element2 element2;
    protected Element3 element3;
    protected Element4 element4;
    @XmlElement(required = true)
    protected String otherID;
    protected String element5;

我使用以下关键字在Google上搜索:java从xsd生成静态变量,并且没有找到有用的东西。有没有办法可以做到这一点,或者我首先需要一个public static String奇怪的东西?

1 个答案:

答案 0 :(得分:1)

如果我说得对,你想为你的模式派生类添加一个静态字段。

您可以做的最简单的事情是使用代码注入器插件:

  

Inserting code with XJC+xsd+jxb using the options " -Xinject-code -extension "

类似的东西:

<jxb:bindings schemaLocation="schema.xsd">
    <jxb:bindings node="/xs:schema/xs:complexType[@name='SomeExtension']">
        <ci:code>
            public static final String EXTENSION_CODE = "NAMEOFTHEEXTENSION";
        </ci:code>
    </jxb:bindings>
</jxb:bindings>