Tuesday, September 4, 2007

Mapping XML- JavaBean

XML makes data portable. The Java platform makes code portable. The Java APIs for XML make it easy to use XML. Put these together, and you have the perfect combination: portability of data, portability of code, and ease of use. In fact, with the Java APIs for XML, you can get the benefits of XML with little or no direct use of XML.The portability and extensibility of both Java and XML make them ideal choices for the flexibility and wide availability requirements of Web applications and services.

The BeanXMLMapping component converts a JavaBean to an XML document and vice versa. By using JavaBean introspection, XML parsers, and DOM APIs, you can develop this component with a bean2Xml() method to represent the received bean as an XML document and a xml2Bean() method to instantiate and populate the proper bean according to the XML document received.

The below code shows a possible implementation for the BeanXMLMapping component. This particular implementation usesthe JOX (Java Objects in XML) library.

Download Dependencies:

jox116.jar
dtdparser121.jar

Code:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.wutka.jox.JOXBeanInputStream;
import com.wutka.jox.JOXBeanOutputStream;

public class BeanXMLMapping {

/**
* Method to generate the Bean object for the given xml String
*/
public static Object xml2Bean(String xml, Class className)
{
ByteArrayInputStream xmlData = new ByteArrayInputStream(xml.getBytes());
JOXBeanInputStream joxIn = new JOXBeanInputStream(xmlData);

try{
return (Object) joxIn.readObject(className);
} catch (IOException exc)
{
exc.printStackTrace();
return null;
}
finally{
try{
xmlData.close();
joxIn.close();
} catch (Exception e){
e.printStackTrace();
}
}

}


/**
* Method to generate the XML document String for the received bean
*/
public static String bean2Xml(Object bean)
{
ByteArrayOutputStream xmlData = new ByteArrayOutputStream();
JOXBeanOutputStream joxOut = new JOXBeanOutputStream(xmlData);
try{
joxOut.writeObject(beanName(bean), bean);
return xmlData.toString();
}
catch
(IOException exc){
exc.printStackTrace();
return null;
}
finally{
try{
xmlData.close();
joxOut.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}

/**
* Method to retrieve the Class name for the given bean object
*/
private static String beanName(Object bean){
String fullClassName = bean.getClass().getName();
String classNameTemp = fullClassName.substring(fullClassName.lastIndexOf(".") + 1, fullClassName.length());
return classNameTemp.substring(0, 1)+ classNameTemp.substring(1);
}
}


The BeanXMLMapping class converts a JavaBean to and from an XML document and provides two methods:

bean2Xml(): generates the respective XML document String for the bean instance
xml2Bean(): creates a bean instance for the XML document String


The below Code shows How to use the BeanXMLMapping class to map the bean to xml and vice varsa.

Code:

public class PersonBean {
private String name;
private Integer age;
private Double salary;
private Integer exp;

public PersonBean(){
this.name="test";
this.age=24;
this.salary=5000.0;
this.exp=2;
}

public PersonBean(String name,Integer age,Double salary,Integer exp){
this.name=name;
this.age=age;
this.salary=salary;
this.exp=exp;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getExp() {
return exp;
}
public void setExp(Integer exp) {
this.exp = exp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String toXML(){
return BeanXMLMapping.bean2Xml(this);
}
public static Object fromXML(String xml){
return (PersonBean) BeanXMLMapping.xml2Bean(xml,PersonBean.class);
}
}


Test Code:

public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
PersonBean bean=new PersonBean();
String xmlStr=bean.toXML();
System.out.println(xmlStr);
}
}


The Above Test Code Generates the Output As:

<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonBean>
<age>24</age>
<exp>2</exp>
<name>test</name>
<salary>5000.0</salary>
</PersonBean>

No comments: