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?

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 138: Still parsing to generate your JavaBeans' XML representation?

Delegate this job to a JavaBean-XML mapping component

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
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. SAX (Simple API for XML), DOM (Document Object Model), XSL (Extensible Stylesheet Language), XSLT (XSL Transformations), SOAP (Simple Object Access Protocol), and BML (Bean Markup Language) are some of the buzzwords associated with XML. This tip brings together the benefits of Java and XML without forcing developers to understand all XML-related buzzwords.

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.

Write the component

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:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources