XML Data-Binding: Comparing Castor to .NET
by Niel Bornstein
|
Pages: 1, 2, 3, 4
You'll see that Castor has generated two Java source files for each element
in the schema. For example, for the dog element, Castor has generated
Dog.java and DogDescriptor.java. But because the
schema also specified some types, they have also been generated,
DogType.java and DogTypeDescriptor.java.
Dog is the class we will use to manipulate the actual
Dog object. DogDescriptor, DogType, and
DogTypeDescriptor will be used internally by Castor to marshal and
unmarshal our objects to and from XML. While Dog proxies the
marshaling methods, it extends DogType, which contains the actual
variables and methods we're interested in.
Here's the generated code for DogType.java, which shows how
Castor structures the generated business classes.
/*
* This class was automatically generated with
* <a href="http://castor.exolab.org">Castor 0.9.3.9+</a>, using an
* XML Schema.
* $Id$
*/
package org.dogshow;
//---------------------------------/
//- Imported classes and packages -/
//---------------------------------/
import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;
import org.exolab.castor.xml.*;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import org.xml.sax.ContentHandler;
/**
*
*
* @version $Revision$ $Date$
**/
public abstract class DogType implements java.io.Serializable {
//--------------------------/
//- Class/Member Variables -/
//--------------------------/
private long _id;
/**
* keeps track of state for field: _id
**/
private boolean _has_id;
private java.lang.String _name;
//----------------/
//- Constructors -/
//----------------/
public DogType() {
super();
} //-- org.dogshow.DogType()
//-----------/
//- Methods -/
//-----------/
/**
* Returns the value of field 'id'.
*
* @return the value of field 'id'.
**/
public long getId()
{
return this._id;
} //-- long getId()
/**
* Returns the value of field 'name'.
*
* @return the value of field 'name'.
**/
public java.lang.String getName()
{
return this._name;
} //-- java.lang.String getName()
/**
**/
public boolean hasId()
{
return this._has_id;
} //-- boolean hasId()
/**
**/
public boolean isValid()
{
try {
validate();
}
catch (org.exolab.castor.xml.ValidationException vex) {
return false;
}
return true;
} //-- boolean isValid()
/**
*
*
* @param out
**/
public abstract void marshal(java.io.Writer out)
throws org.exolab.castor.xml.MarshalException,
org.exolab.castor.xml.ValidationException;
/**
*
*
* @param handler
**/
public abstract void marshal(org.xml.sax.ContentHandler handler)
throws java.io.IOException,
org.exolab.castor.xml.MarshalException,
org.exolab.castor.xml.ValidationException;
/**
* Sets the value of field 'id'.
*
* @param id the value of field 'id'.
**/
public void setId(long id)
{
this._id = id;
this._has_id = true;
} //-- void setId(long)
/**
* Sets the value of field 'name'.
*
* @param name the value of field 'name'.
**/
public void setName(java.lang.String name)
{
this._name = name;
} //-- void setName(java.lang.String)
/**
**/
public void validate()
throws org.exolab.castor.xml.ValidationException
{
org.exolab.castor.xml.Validator validator =
new org.exolab.castor.xml.Validator();
validator.validate(this);
} //-- void validate()
}
As you can see, the generated code gives us a JavaBeans interface for all
the instance variables, so getXXX() and setXXX()
methods are there. It also has included a helper instance variable called
_has_id to track whether the instance variable _id,
which is a long, has a value. Castor has ways to generate the code using wrapper
objects which would make this unnecessary; we won't go into how to do this, but
if you're interested in learning more about Castor, look at Brett's book or Dion
Almaer's OnJava.com article XML Data
Binding with Castor.