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
IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Projects for CSE It gives you tips and rules that is progressively critical to consider while choosing any final year project point.
ReplyDeleteSpring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
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
Excellent goods from you, man. I have understood your stuff previous to and you are simply extremely fantastic. I actually like what you’ve obtained right here, really like what you are saying and the way in which by which you are saying it. You are making it enjoyable and you still care to stay sensible. I can’t wait to read much more from you. This is really a wonderful site. Instrumentation and Control Engineering
ReplyDeleteThankss 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