Recent articles:
Popular archives:
Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can,
or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing
heavily in Java's future as a platform for platforms
Also see:
Discuss: Java: A platform for platforms?
By using Remote Method Invocation (RMI) in distributed Java application development, no low-level socket or network communication code is involved. The code remains at a higher level, leveraging its use of RMI classes. Similar gain comes with the use of Enterprise JavaBeans (EJB) technology, freeing developers from several low-level coding aspects (transaction, recovery, and activation). With this tip's JavaBean-XML mapping component, developers don't directly deal with XML-related APIs.
Note: You can download this tip's source code from Resources.
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 toXML() method to represent the received bean as an XML document and a fromXML() method to instantiate and populate the proper bean according to the XML document received.
Listing 1 shows a possible implementation for the BeanXMLMapping component. This particular implementation uses the JOX (Java Objects in XML) library. You can develop other implementations
of this component using other APIs. I selected JOX for its simple, reusable solution.
Listing 1. BeanXMLMapping component
import com.wutka.jox.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class BeanXMLMapping {
/**
* Retrieves a bean object for the
* received XML and matching bean class
*/
public static Object fromXML(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();
}
}
}
/**
* Returns an XML document String for the received bean
*/
public static String toXML(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();
}
}
}
/**
* Find out the bean class name
*/
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: