Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate additional custom method with jaxb-xjc

There's some way to generate a custom method within an class generated with JAXB.

I search around tutorials, including oracle's tutorial, but I didn't find clear instructions how can I custom methods to a generated class described on XML Schema.

like image 304
Andre Pastore Avatar asked Feb 02 '11 14:02

Andre Pastore


People also ask

How do I use JAXB XJC?

Open a command prompt. Run the JAXB schema compiler, xjc command from the directory where the schema file is located. The xjc schema compiler tool is located in the app_server_root \bin\ directory. Use the generated JAXB objects within a Java application to manipulate XML content through the generated JAXB classes.

Is XJC a JAXB?

Use the Java™ Architecture for XML Binding (JAXB) tools to generate Java classes from an XML schema with the xjc schema compiler tool.

How do I generate Java classes from XSD using JAXB maven?

Just build the maven project using mvn clean install and you will see java classes generated in target/generated-sources/jaxb directory.

Which component or tool in JAXB can generate Java files from schemas?

You can create an XML schema document from an existing Java application that represents the data elements of a Java application by using the JAXB schema generator, schemagen command-line tool. The JAXB schema generator processes either Java source files or class files.


2 Answers

I have found the following to be the best way to add custom behavior:

https://javaee.github.io/jaxb-v2/doc/user-guide/ch03.html#compiling-xml-schema-adding-behaviors

You can implement the custom behavior for a JAXB generated class BaseType in a custom class BaseTypeExt that extends the JAXB generated class BaseType.

You also tell JAXB to use your extended class BaseTypeExt (instead of the BaseType class it extends) as the base class for all classes that normally would have extended the BaseType class.

I used this successfully to override the toString() method in my generated class BaseType.

This seems simpler than writing an XJC plugin.

like image 198
Farrukh Najmi Avatar answered Oct 30 '22 17:10

Farrukh Najmi


You can write an XJC plugin:

  • http://weblogs.java.net/blog/kohsuke/archive/2005/06/writing_a_plugi.html
like image 30
bdoughan Avatar answered Oct 30 '22 17:10

bdoughan