简体   繁体   English

如何从XSD编辑生成的Java类

[英]How to edit generated java classes from xsd

I am using maven-jaxb2-plugin to create java classes from xsd. 我正在使用maven-jaxb2-plugin从xsd创建Java类。 In xsd I have defined two status: Status1 and Status2. 在xsd中,我定义了两个状态:Status1和Status2。 This two classes has a lot of similar variables. 这两个类有很多相似的变量。 Because I cant edit xsd is there a option to create class Status in my project and then add change generated class to extend this class? 因为我无法编辑xsd,所以可以在我的项目中创建类Status,然后添加更改生成的类以扩展该类吗?

Example

now: 现在:

Class Status1 {...}

Class Status2 {...}
  • this is generated by maven-jaxb2-plugin 这是由maven-jaxb2-plugin生成的

What I want: 我想要的是:

Class Status1 extends Status {...}

Class Status2 extends Status {...}
  • this is generated by maven-jaxb2-plugin 这是由maven-jaxb2-plugin生成的

Class Status {...} - my own class Class Status {...} -我自己的班级

I hope this can be done by bindings but I dont know how 我希望这可以通过绑定来完成,但我不知道如何

UPDATE: 更新:

second option how to achieve this question is to add to class Status1 new variable. 第二个选择如何解决此问题的方法是在类Status1中添加新变量。 Is this possible ? 这可能吗 ?

What I want: 我想要的是:

Class Status1 {... NewVariable newVariable ...}

Class Status2 {...}
  • this is generated by maven-jaxb2-plugin 这是由maven-jaxb2-plugin生成的

ok I found it: I use extend: 好的,我找到了:我使用了extend:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    jaxb:version="2.1" xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
    jaxb:extensionBindingPrefixes="inheritance">
    <jaxb:bindings schemaLocation="mySchema.xsd">
        <jaxb:bindings node="xsd:complexType[@name='StatusType']">
            <inheritance:extends>my.package.Status1</inheritance:extends>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

and in JAXB2 plugin I need to add: 在JAXB2插件中,我需要添加:

                    <extension>true</extension>
                    <args>
                        <arg>-Xinheritance</arg>
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.4</version>
                        </plugin>
                    </plugins>

For the first part of the question see Specifying Super Class of a JAXB-Generated Class . 对于问题的第一部分,请参见指定JAXB生成的类的超类

For the second part see Generate additional custom method with jaxb-xjc . 对于第二部分,请参阅使用jaxb-xjc生成其他自定义方法 It's about adding custom methods. 这是关于添加自定义方法的。 But custom fields handling should be similar. 但是自定义字段的处理应该相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM