Feb 15, 2009

XMLBeans in Eclipse

How to setup an XMLBeans project in Eclipse.

The target is to have a sample XMLBeans project setup in eclipse and access an XML via java objects.

As always, this blog is for techie people, so I will get right to the steps, since that's what we are interested in rather than what the technology is. If we did not know what it is, we would not have googled and reached here.

  1. Download XMLBeans from apache. The version I downloaded is 2.4.0
    Extract the zip or tar.gz file, lets call is {xmlbeans.home}
  2. Open Eclipse (I created a separate workspace for this) and create a new java project (not a J2EE yet) named XMLBeanDemo1AV, Lets set up the code structure...
    Go in the Project properties > Java build path > Source tab and make the default output folder as XMLBeanDemo1AV/build. This will make the build directory 'invisible' to eclipse (but its there) and will keep all the compiled source in there.
    The sample code I am using is from the XMLBeans tutorial. (Local copy)
    Make a directory 'schemas' and copy the easypo.xsd file from the {xmlbeans.home}/schemas folder.
    Edit these lines from the top of this file so that you can have your own package naming convention, I have decided on ... 'org.avxmlbeans.easypo'
    xmlns:po="http://avxmlbeans.org/easypo"
    targetNamespace="http://avxmlbeans.org/easypo"
    (source for easypo.xsd)
    Copy a sample build.xml from the {xmlbeans.home}/samples/Any folder into the eclipse project. Rename the project name line (source for build.xml)
    Correctly assign the {xmlbeans.home} variable, to point to the home folder.
  3. Now lets tell eclipse to build the project via Ant, by going to Project > Properties > Builders.
    New > Ant Builder (give a name ... my name is Ant builder)
    In the Main tab select the build.xml by clicking on 'Browse Workspace' in the Buildfile section.
    In the Classpath tab select the {anthome} where ant is installed by selecting the Ant home... button.
    Click ok.
    build the project. (project > build)
    Now you should see from the filesystem view that class files have been created in the build folder and source files have been created in the src folder in eclipse with the correct package structure.
  4. Not removing the 'Java Builder' is a good idea, since that way we will get the IDE features like typeahead and compilation error alerts much earlier. For them to work we will need to include xbean.jar and jsr173 api.jar via the project > properties > java build path > libraries > add external jar..
  5. Look at the generated stub code and java objects as mentioned in the tutorial. and then move on to the next step of creating the POUpdater.java class and copying the code in it. (source for POUpdater.java)
  6. Make sure that you change the package name for easypo.*; and the path of the XML to "./xmls/easypo.xml" (source for easypo.xml). Since I wanted to run this from within eclipse I gave the arguments in the following fashion
    File poXmlFile = new File("./xmls/easypo.xml");
    String updatedPoXml = addLineItem(poXmlFile, "a new item", "5.0", "20.00", "6");

    Running the project via the Run Dialog... now will give you ClassNotFoundException. For that you will need to add a few entries on it's Classpath tab. Add an external jar.. (schemas.jar) and hit Advanced... add a folder and add the build/classes folder.
    Run now, and see the output in Eclipse's console.
  7. The new LineItem should be added at the bottom of the XML if you used the addLineItem() function, otherwise using the Cursor method it should be at the top.
These steps should get us going with the setup of a barebones XMLBeans project in Eclipse. The rest should be pretty easy to explore from the docs.

Please do post comments, suggestions and feedback.
Ömer Faruk said...

this article is wonderful, thank you... :)

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search