简体   繁体   中英

How to add extra methods in generated classes using jaxb-xjc schema

Is there's a way to have extra methods in an class generated with JAXB ... To be more specific I would like make changes in my .xsd files and not code some methods in a "first.java" class and then make my .xsd generated classes inherit from "first.java" .. My goal is to modify only my .jaxb file .. so can we create methods with .xsd ?

I have an ".xsd" file that generates a class. And I want to know if it's possible to add to this generated class some other methods besides the getters and setters. I search around tutorials and some StackOverflow topics but I didn't find instructions on how can I add custom methods to a generated class described on XML Schema.

Here's my .xsd file :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.1">
    <xs:element name="Myclass" />
    <xs:complexType name="Myclass">
        <xs:complexContent>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

And here's the class generated with this .xsd file :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Myclass")
public class Myclass
{

}

Here's an example of a class that I want to generate with

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Myclass")
public class Myclass
{
     public String method(){
                return "Hello";
       }
}

Thank's in advance :)

If you receive the XSDs from a client and they can change often, I would recommend not making changes to the XSDs as a design decision. I would recommend the java route as mentioned on the JAXB documentation: https://javaee.github.io/jaxb-v2/doc/user-guide/ch03.html#compiling-xml-schema-adding-behaviors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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