apis, java

Binding XML to Java

Manipulating XML data easily and efficiently in Java remains an important problem. Numerous approaches to XML binding exist in the industry, including DOM, JAXB, XML Beans, Castor, and SDO and so on. In the article “Binding XML to Java” the authors explore how the Eclipse Modeling Framework, EMF, solves the XML binding problem in a number of interesting ways, and compare that to the alternatives.


“…The model that is used to represent models in EMF is called Ecore, and since Ecore is itself a model, it is called a Meta model, i.e., the model of a model. EMF supports this core meta model API, Ecore, analogous to XML Schema, as well as a core instance data model API, EObject, analogous to DOM Node. Ecore is to abstract syntax what XML Schema is to concrete syntax, i.e., a unifying meta model. But rather start with vague abstractions, it seems best to start from something well known and concrete on which to draw comparisons… “


Read More

Standard
apis, java

Serializing Java Objects with XStream – A Short tutorial

 

This is a very quick introduction to XStream. Skim read it to get an idea of how simple it is to convert objects to XML and back again. I am sure you’ll have questions afterwards.XStream

Create Classes to be serialized:

Here are a couple of simple classes. XStream can convert instances of these to XML and back again.

public class Person{

private String firstName;
private String lastName;
private PhoneNumber phone;
private PhoneNumber fax;

public Person(String firstName, String lastName){

setFirstName(firstName);

setLastName(lastName);

}

public String getFirstName(){ return firstName; }

public void setFirstName(String firstName){ this.firstName = firstName; }

public String getLastName(){ return lastName; }

public void setLastName(String lastName){ this.lastName = lastName; }

public PhoneNumber getPhone(){ return phone; }

public void setPhone(PhoneNumber phone){ this.phone = phone; }

public PhoneNumber getFax(){ return fax; }

public void setFax(PhoneNumber fax){ this.fax = fax; }

}

 

public class PhoneNumber{

private int code; private String number;

public PhoneNumber(int code, String number){

this.code = code; this.number = number;

}

}

 

Note: Notice that the fields are private. XStream doesn’t care about the visibility of the fields. No getters or setters are needed. Also, XStream does not limit you to having a default constructor.

Initializing XStream:

To use XStream, simply instantiate the XStream class.

XStream xstream = new XStream();

You require xstream-[version].jar and xpp3-[version].jar in the classpath. XPP3 is a very fast XML pull-parser implementation. If you do not want to include this dependency, you can use the standard JAXP DOM parser instead.

XStream xstream = new XStream(new DomDriver());

Now, to make the XML outputted by the XStream more concise, you can create aliases for your custom class names to XML element names. This is only type of mapping required to use XStream and also this is optional.

xstream.alias(“person”, Person.class);

xstream.alias(“phone-number”, PhoneNumber.class);

Note: This step is optional. Without it XStream would work fine, but the XML element names would contain the fully qualified name of each class (including package) which would bulk up the XML a bit.

 

Serializing an object to XML:

Lets write a test class, in which we create a Person class and populate its fields:

Person sam = new Person(”Syed”, “Aslam”);

sam.setPhone(new PhoneNumber(95, “253265″));

sam.setFax(new PhoneNumber(95, “423140″));

Now, to convert it to XML, all you have to do is make a call to XStream:

XStream xstream = new XStream();String xml = xstream.toXML(sam);

And that’s it. The test class would look like this:

 

import com.thoughtworks.xstream.*;

public class XStreamTest{

public static void main(String[] args){

XStream xstream = new XStream();

Person sam = new Person(”Syed”, “Aslam”);

sam.setPhone(new PhoneNumber(95, “253265″));

sam.setFax(new PhoneNumber(95, “423140″));

String xml = xstream.toXML(sam);

System.out.println(xml);

}

}

 

The resulting XML would look like this:

<person>
  <firstname>Syed</firstname>
  <lastname>Aslam</lastname>
  <phone>
    <code>95</code>
    <number>253265</number>
  </phone>
  <fax>
    <code>95</code>
    <number>423140</number>
  </fax>
</person>

It’s that simple. Look how clean the XML is.

Deserializing an object back from XML

To reconstruct an object, purely from XML:

Person newPerson = (Person) xstream.fromXML(xml);

 

 

 

 

 

Standard