Sunday, December 10, 2006

Java: JaxB Part 2 - Using JaxB to Create Java Classes from an XML Schema

Last article I created a XML Schema to use in my example program. I created an XML file that would represent the file structure. My next step in developing my example application is to run JaxB to create a series of Java classes to create the XML file.

The JaxB implementation I used was part of the Java Web Services Developer Pack 2.0, which I installed to directory C:\Sun\jwsdp-2.0

Once installed, I set the following environment variables:

JAVA_HOME=C:\Program Files\Java\jdk1.5.0_09
ANT_HOME=C:\Sun\jwsdp-2.0\apache-ant
ANT_OPTS=-Djava.endorsed.dirs=C:\Sun\jwsdp-2.0\jaxp\lib\endorsed;C:\Sun\jwsdp-2.0\jaxp\lib
JWSDP_HOME=C:\Sun\jwsdp-2.0
PATH= (Added) C:\Sun\jwsdp-2.0\jwsdp-shared\bin; C:\Sun\jwsdp-2.0\jaxb\bin\

Now, I can compile XML schemas from the command line by using the XJC.BAT command. Since I had created a Java project already containing the schema, I needed to use a command prompt to change to that directory and run XJC against the resulting .XSD file created by the Web Tools Project XML Schema designer. In order to do that and create the Java classes, I run the following commands, where C:\WTP is the location of my Eclipse installation, C:\WTP\WORKSPACE is the location of my workspace and project files.

cd c:\WTP\workspace\FileStructureXML
xjc.bat –p com.digiassn.blogspot.filestructure FileStructure.xsd

That’s it, the following files were created from the above commands:
C:\wtp\workspace\FileStructureXML\com\digiassn\blogspot\filestructure\
C:\wtp\workspace\FileStructureXML\com\digiassn\blogspot\filestructure\FilesInFolder.java
C:\wtp\workspace\FileStructureXML\com\digiassn\blogspot\filestructure\Lfile.java
C:\wtp\workspace\FileStructureXML\com\digiassn\blogspot\filestructure\ObjectFactory.java
C:\wtp\workspace\FileStructureXML\com\digiassn\blogspot\filestructure\package-info.java

And next article, I will wrap this up and show the completed application.

No comments: