In this tutorial I will show how to generate the Java source code from XSD schema. I will use jaxb2-maven-plugin to generate the code using XSD file which will be declared in pom.xml to make it part of build, so when maven build is executed it will generate the java code using XSD. Class generation can be controlled in plugin configuration.
schemaDirectory: This is the directory where I keep my schema (XSD file).
outputDirectory: This is the java source location where I want to generate the Java files. If it is not given then by default it will be generate inside target folder.
clearOutputDir: If this property is true then it will generate the classes on each build otherwise it will generate only if output directory is empty.
Below is the snap of code from generated classes. Getters/Setters are also generated but not included in example.
You may be interested in my other post on generating Java classes using annotation processor.
https://www.thetechnojournals.com/2019/12/annotation-processor-to-generate-dto.html
Maven changes (pom.xml)
Include below plugin in your pom.xml. Here we have done some configuration under configuration section as given below.schemaDirectory: This is the directory where I keep my schema (XSD file).
outputDirectory: This is the java source location where I want to generate the Java files. If it is not given then by default it will be generate inside target folder.
clearOutputDir: If this property is true then it will generate the classes on each build otherwise it will generate only if output directory is empty.
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>1.6</version> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${project.basedir}/src/main/resources/schema</schemaDirectory> <outputDirectory>${project.basedir}/src/main/java</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </plugin>
XSD Schema (employee.xsd)
Below is the XSD schema which will be used to generate the Java class.<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://myexample.com/employee" targetNamespace="http://myexample.com/employee"> <xs:complexType name="employee"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:int"/> <xs:element name="organization" type="xs:string"/> <xs:element name="salary" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:schema>XSD location is as given below in my application.
Generating Java classes
After setting up the above code and configuration, we can run the maven build by executing "men clean install". It will generate the Java classes using the XSD schema. Below is the generated code package structure after the build execution.Below is the snap of code from generated classes. Getters/Setters are also generated but not included in example.
- Employee.java
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "employee", propOrder = { "name", "age", "organization", "salary" }) public class Employee { @XmlElement(required = true) protected String name; protected int age; @XmlElement(required = true) protected String organization; @XmlElement(required = true) protected BigDecimal salary;
- ObjectFactory.java
@XmlRegistry public class ObjectFactory { /** * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.myexample.employee * */ public ObjectFactory() { } /** * Create an instance of {@link Employee } * */ public Employee createEmployee() { return new Employee(); } }
- package-info.java
@javax.xml.bind.annotation.XmlSchema(namespace = "http://myexample.com/employee") package com.myexample.employee;That's all. Usually you may have a need to generate the class this way when you are creating some SOAP service client or you can use it to just create the Java POJO classes also.
You may be interested in my other post on generating Java classes using annotation processor.
https://www.thetechnojournals.com/2019/12/annotation-processor-to-generate-dto.html
ReplyDeleteGreat post. Thanks for sharing a valuable information.....
Angularjs Training in Bangalore
Angularjs classes in pune
Angularjs Training in hyderabad
Angularjs Training in Gurgaon
Angularjs Training in delhi
Thankss for a great read
ReplyDeletevenlaQmenshe Brody Fokused https://wakelet.com/wake/Sj_HXd0MUa7zSnoaWGXcr
ReplyDeletegritapbrownez
granliWin-ka Nathan Plowden click
ReplyDeleteclick here
link
download
croclojuslo
evactinc_zo Robert Ritchie DesignCAD 3D Max
ReplyDeleteAVG PC TuneUp
CCleaner pro
tlinbucktina
Nice blog ! Thank you so much for compiling such a great article! Clearly, this is an excellent post. I appreciate the insightful and useful information you have offered here. custom erp software
ReplyDeleteYalova
ReplyDeleteHatay
MuÅŸ
Bursa
Mersin
YUSCG1
bingöl
ReplyDeleteelazığ
hakkari
sakarya
erzincan
WYXG
elazığ
ReplyDeletegümüşhane
kilis
siirt
sakarya
J1VVYF
görüntülü.show
ReplyDeletewhatsapp ücretli show
PLV0N
https://titandijital.com.tr/
ReplyDeleteısparta parça eşya taşıma
ankara parça eşya taşıma
izmir parça eşya taşıma
diyarbakır parça eşya taşıma
OYWQYC
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
51OYZ0
1BA66
ReplyDeleteKütahya Evden Eve Nakliyat
Siirt Evden Eve Nakliyat
Siirt Lojistik
Sakarya Evden Eve Nakliyat
Tokat Parça Eşya Taşıma
8ACCC
ReplyDeleteErzincan Şehirler Arası Nakliyat
Van Şehirler Arası Nakliyat
Etlik Boya Ustası
Tokat Şehir İçi Nakliyat
Bilecik Lojistik
Batman Lojistik
Bilecik Şehirler Arası Nakliyat
Çerkezköy Marangoz
Malatya Şehir İçi Nakliyat
EFE14
ReplyDeleteEdirne Şehirler Arası Nakliyat
Van Parça Eşya Taşıma
Amasya Lojistik
Yenimahalle Parke Ustası
Çerkezköy Koltuk Kaplama
Denizli Lojistik
Adana Şehirler Arası Nakliyat
Ünye Marangoz
Ünye Çekici
60B6D
ReplyDeleteKütahya Evden Eve Nakliyat
Balıkesir Lojistik
Çerkezköy Marangoz
Çorum Parça Eşya Taşıma
Okex Güvenilir mi
Siirt Parça Eşya Taşıma
Elazığ Şehir İçi Nakliyat
Giresun Şehirler Arası Nakliyat
Karabük Parça Eşya Taşıma
633B9
ReplyDeleteKütahya Parça Eşya Taşıma
Ordu Parça Eşya Taşıma
Tokat Şehir İçi Nakliyat
Aksaray Evden Eve Nakliyat
AAX Güvenilir mi
Silivri DuÅŸa Kabin Tamiri
Tekirdağ Parke Ustası
Adıyaman Evden Eve Nakliyat
Gölbaşı Fayans Ustası
423C1
ReplyDeleteMuş Parça Eşya Taşıma
Aion Coin Hangi Borsada
Maraş Parça Eşya Taşıma
NiÄŸde Lojistik
Dxgm Coin Hangi Borsada
Denizli Evden Eve Nakliyat
Kırşehir Şehirler Arası Nakliyat
Çerkezköy Bulaşık Makinesi Tamircisi
Tekirdağ Fayans Ustası
55BE7
ReplyDeleteBitlis Şehirler Arası Nakliyat
Ağrı Şehir İçi Nakliyat
Trabzon Şehir İçi Nakliyat
Hamster Coin Hangi Borsada
Mamak Parke Ustası
Tokat Şehir İçi Nakliyat
Kars Evden Eve Nakliyat
Wabi Coin Hangi Borsada
Çerkezköy Boya Ustası