Category: XMLBeans


Scomp is an Apache XMLBeans command line tool that will take your XML schemas and turn them into Java XMLBeans classes and metadata. Quite handy when you want to compile your schemas into a JAR file to create a reusable schema library for your Java projects.

For Windows, create an environment variable name: XMLBEANS_LIB value: C:\xmlbeans-2.5.0\lib

My typical command line usage is as follows:

>cd C:\xmlbeans-2.5.0\bin

>scomp -out output.jar -compiler “C:\Program Files\Java\jdk1.6.0_05\bin\javac” -javasource 1.5 -allowmdef -verbose ..\schemas\schema1.xsd ..\schemas\schema2.xsd

XMLBeans and NetBeans

Apache XMLBeans allows you to take XML schemas and compile them into Java objects that represent schema types. Very useful in Java projects that need to work with data in XML format such as SOAP based web services and web service clients.

Sometimes it’s nice to integrate XML schema compilation (using XMLBeans) with a NetBeans project. I use it as an alternative to running the XMLBeans scomp tool from the command line. Don’t ask why I use NetBeans – it’s a love/hate relationship but at the moment it has the best plug-ins to support my dev projects so I have shunned Eclipse for now.

Full credit goes to this helpful article I found online http://lajavaloca.wordpress.com/2008/05/26/xmlbeans-with-netbeans-thats-a-lot-of-beans/ which outlines the approach I use.

  1. Download the latest Apache XMLBeans binaries from http://apache.wildit.net.au/xmlbeans/binaries/
  2. Extract the contents to a location of your choice E.g. C:\xmlbeans-2.5.0
  3. Copy the following JAR files jsr173_1.0_api.jar and xbean.jar from C:\xmlbeans-2.5.0\lib into your projects lib folder. Add both JAR files to the project in the IDE.
  4. Now you need to edit your Ant build file build.xml. If you didn’t even realise you had an Ant build file in your NetBeans project, never fear – now is a good time to get to know. Locate the file build.xml and notice that it contains the following line  <import file=”nbproject/build-impl.xml”/>. Just as a heads up, don’t go modifying build-impl.xml because this is an auto-generated file and your good work may get overwritten. We will be modifying build.xml instead. Add the following:

<taskdef name=”xmlbean”
classname=”org.apache.xmlbeans.impl.tool.XMLBean”
classpath=”lib/xbean.jar:lib/jsr173.jar”/>

<target name=”-pre-compile”>
<antcall target=”gen-schema-source”/>
</target>
<target name=”gen-schema-source”>
<xmlbean
srconly=”true”
verbose=”true”
srcgendir=”src/java”
classgendir=”build/classes”>
<fileset dir=”schemas” includes=”**/*.xsd **/*.xsdconfig”/>
</xmlbean>
</target>

Note. The above snippet assumes you have created a directory called “schemas” containing your .xsd files in your project’s root directory.

Only annoying thing I have found with this approach is when you are compiling a large collection of schemas. In this case you don’t want to be regenerating the XMLBeans source and binaries with every compilation of your project. Instead, compile your schemas once and don’t rebuild them again unless you make changes to them. You can just comment out the above snippet in the build file after the first successful build to achieve this.

For a more detailed look at XMLBeans ant task parameters and usage see http://xmlbeans.apache.org/docs/2.0.0/guide/antXmlbean.html.