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”

<target name=”-pre-compile”>
<antcall target=”gen-schema-source”/>
<target name=”gen-schema-source”>
<fileset dir=”schemas” includes=”**/*.xsd **/*.xsdconfig”/>

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.