How to generate Java files using JAXB xjc command


Here is one simple example to generate java class files from xsd file using JAXB xjc command.

Steps

1. Create a Sample xsd file (Example order.xsd)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Order" type="Order"/>
  <xs:complexType name="Order">
    <xs:sequence>
      <xs:element name="orderNumber" type="xs:long"/>
      <xs:element name="orderItem" type="orderItem" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="orderItem">
    <xs:sequence>
      <xs:element name="orderItemNumber" type="xs:long"/>
      <xs:element name="orderItemName" type="xs:string" minOccurs="0"/>
      </xs:sequence>
  </xs:complexType>
</xs:schema>

2. Create a directory which you want to save java files (eg javafiles)

3. Go to command prompt and use following command to generate java classess (Make sure that java class path set up is there to get xjc tool)


xjc -d javafiles order.xsd

 



4. We can see the console like below and all the classes are generated in the javafiles directory


$ xjc -d javafiles order.xsd
parsing a schema...
compiling a schema...
generated/ObjectFactory.java
generated/Order.java
generated/OrderItem.java

 



Reference


http://jaxb.java.net/2.2.4/docs/xjc.html

No comments:

Post a Comment

12 classic String-based Java interview questions with simple explanations and code.

  1️⃣ Check if a String is a Palindrome Problem Given a string, check if it reads the same forward and backward. Example: "madam...

Featured Posts