Java XML with Betwixt

Having first being introduced to the world of parsing XML into objects in .NET a few years back, I was hooked immediately. The power of taking any stream and converting it to an object within a couple of lines of code (and then back from an object to a file / stream) was immense.

Moving into the world of Java, I can remember thinking well this will be out the box. Unfortunately it wasn’t (or at least I couldn’t find it). Just before Christmas though I stumbled across Betwixt when doing some xml parsing for another Java project. Whilst not quite as straight-forward as the .NET world, it pretty good, and I like it. I meant to blog about it back then, but didn’t get round to it. I had to do some more XML-Java parsing today, so had to refresh my memory – hence the nudge for doing this blog post.

A Simple Example

Betwixt as available from Apache Commons. After downloading and unzipping the library, it also requires a number of other Apache Common libraries in order to work – something that took me a while to figure out. I believe the other jars required are:

  • commons-beanutils.jar
  • commons-beanutils-core.jar
  • commons-digester.jar
  • commons-io.jar
  • common-logging.jar

Once these are added to our Java project, we can get on with our simple example. Its worth noting at this point that there are a few example available from the Betwixt website.

The Example XML

The example present will just be reading some XML. The XML in question looks like this:

<xml version=’1.0’?>
<locations>
   <location>home</location>
   <location>work</location>
</locations>

The POJO to Represent the XML

So first thing would be to create an object to represent out location element. This class looks like:

public class LocationBean {
   public LocationBean() {
   }

   private List location = new ArrayList<String>();

   public void setLocation(List<String> location) {
      this.location = location;
   }

   public List getLocation() {
      return location;
   }

   public void addLocation(String location) {
      this.location.add(location);
   }
}

Betwixt Configuration File for Handling Lists

In order to handle the List, we also need to create a Betwixt configuration file – something I hadn’t realised until reading this article from ‘Anonymity Re-Discovered!’ blog.

So creating this ‘LocationBean.betwixt’ file at the same level as the LocationBean class, it takes the following structure:

<?xml version='1.0' ?>
<info>
   <element name="locations">
      <element name='location' property='location' updater='addLocation' />
   </element>
</info>

Main Program Code

Finally, I now just need to implement my main java code to point the stream to the Betwixt XML Reader:

StringReader locationXml = new StringReader("<?xml version='1.0' ?><locations><location>work</location><location>home</location></locations>");
BeanReader xmlReader = new BeanReader();

try {
   xmlReader.registerBeanClass("locations", LocationBean.class);
   LocationBean markBean = (LocationBean) xmlReader.parse(locationXml);
   System.out.println("Location = " + markBean.getLocation().size());
} catch (Exception e) {
   System.out.println("Exception raised: " + e.getMessage());
}

When not handling lists, the code is a lot simpler, but I think that the example on the Betwixt site is more than adequate to cover this.

Advertisements

2 thoughts on “Java XML with Betwixt

  1. Pingback: HTML Scripts Tips and Secrets » Blog Archive » Java XML with Betwixt « Gregor Bowie's Blog

  2. Pingback: seo news

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s