Tuesday, October 5, 2010

How to setup command line JAXB schema compiler (XJC)

When you wanted to write a simple hello world program, its always a pain in the neck to figure out how to setup the XJC tool to run in command line to get it started.


You can download the tool separately in the following site:
https://jaxb.dev.java.net/


Click the "Download Now 2.*" on the latest version.
Next page, Click the "Download Now 2.*"
Then double click the file to extract it in place.
Set the JAXB_HOME path to the folder in which you have extracted the content (e.g. C:\java\jaxb)
Set the JAVA_HOME path to where the Java 1.5 or greater is installed (provided that this is already installed!)


Now you are all set.


Create a sample XML schema file named hello.xsd
and then run the following command from the command line


%JAXB_HOME%\bin\xjc -p hello C:\sample\schema\hello.xsd


Walla...the Java files are now generated.


Sample XML Schema:
https://jaxb.dev.java.net/tutorial/section_1_3-Hello-World.html



<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
            jxb:version="2.0">

<xsd:element name="Greetings" type="GreetingListType"/>

<xsd:complexType name="GreetingListType">
  <xsd:sequence>
    <xsd:element name="Greeting" type="GreetingType"
                 maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="GreetingType">
  <xsd:sequence>
    <xsd:element name="Text" type="xsd:string"/>
  </xsd:sequence>
  <xsd:attribute name="language" type="xsd:language"/>
</xsd:complexType>

</xsd:schema>

No comments:

Post a Comment