Showing posts with label JAXB. Show all posts
Showing posts with label JAXB. Show all posts

Saturday, May 16, 2009

Caster - XML/JAVA Object Mapping

Castor is an Open Source data binding framework for Java[tm].Castor provides Java-to-XML binding

Castor XML can marshal almost any "bean-like" Java Object to and from XML. In most cases the marshalling
framework uses a set of ClassDescriptors and FieldDescriptors to describe how an Object should be marshalled
and unmarshalled from XML.

The mapping rules can be mentioned in xml files which makes it easy for customization.

It also has features for java code generation from xml.

http://www.castor.org/

JAXB -Xml/Java Mapping

After looking at many options which helped me convert java classes into xml and vice versa, i found this JAXB very exciting.The Java Architecture
for XML Binding provides a powerful and practical way of working with XML content from within Java applications. The newly released JAXB 2.0 offers
many new features, including full support of all XML Schema features, significantly fewer generated classes, generated classes that are easier to
manipulate, and a more flexible validation mechanism.

JAXB prerequisites

To get started with JAXB 2.0 you need:

1 Java Platform, Standard Edition 5: JAXB 2.0 relies heavily on features of Java SE 5, such as annotations and generics
2. An implementation of JAXB 2.0

I chose this implementation - https://jaxb.dev.java.net/

To start with the JAXb we first need to generate java classes using the xsd.This can be easily done ussing the xjc compiler

syntax : $xjc .xsd -p -d

Once this command are exceuted the classes are generated in teh given package.These can be used to marshel and unmarshel the xml quite easily.

First we create a JAXBContext

JAXBContext jaxbContext = JAXBContext.newInstance("package.of.the.generated.classes");

Then carete a unmarsheller using the context

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

Use the unmarshellor to get the data objects for the xml

DataObj data = (DataObj) unmarshaller.unmarshal(url);

url is URL which returns the xml response.Also xml can be given from a file directly

this data object can ve used to access all other valuse presnt in the xml response.

Ref : http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
http://www.javaworld.com/javaworld/jw-06-2006/jw-0626-jaxb.html



Wednesday, November 12, 2008

XStream - Convert XMl to Java & Vice Versa

XStream is a simple library to serialize objects to XML and back again.
XStream xstream = new XStream();
Person joe = new Person("Joe", "Walnes");
String xml = xstream.toXML(joe)
The resulting XML looks like this:
<code>
<person>
<firstname>Joe</firstname>
<lastname>Walnes</lastname>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
</person></code>
Deserializing an object back from XML
Person newJoe = (Person)xstream.fromXML(xml);
Reference :http://xstream.codehaus.org/

 
Free Domain Names @ .co.nr!