Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

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

Delegate this job to a JavaBean-XML mapping component

  • Print
  • Feedback

Page 2 of 2

  • toXML(): generates the respective XML document String for the bean instance
  • fromXML(): creates a bean instance for the XML document String


Use the component

Listing 2 shows AccountHistoryContext, a bean class empowered with toXML() and fromXML() capabilities. The JavaBean class simply delegates its toXML() and fromXML() methods to the BeanXMLMapping component.

Listing 2. AccountHistoryContext bean

public class AccountHistoryContext{
  private String dateFrom;
  public String getDateFrom()  { return dateFrom; }
  public void setDateFrom(String s)  { dateFrom = s;  } 
  ...
  // Other attributes with their get and set methods
  public String toXML()   {
    return BeanXMLMapping.toXML(this);
  }
  public static AccountHistoryContext fromXML (String xml)   {
      return (AccountHistoryContext)  
        BeanXMLMapping.fromXML (
           xml, AccountHistoryContext.class);
  }
}


Figure 1 represents the mapping between a generic JavaBean class and its respective generic XML document.

Figure 1. JavaBean-XML document mapping. Click on thumbnail to view full-size image.

JavaBean to XML

The XML document's first line is the XML declaration, which defines the document's XML version. In this case, the document conforms to XML Specification 1.0 (<?xml version="1.0" encoding="ISO-8859-1"?>).

The next line defines the document's first element (the root element). This is the JavaBean name (<Bean>).

The next lines define the root's (JavaBean's) child elements. These are the bean attributes available through get methods. If the attribute is a basic type (e.g., String, int), a node with the attribute name is generated. If a bean has a get method for a nested bean, nested child elements are generated.

Figure 2 shows a ContactInfo bean object, its class, and the XML document generated by the toXML() method invocation.

Figure 2. toXML() method invocation

XML to JavaBean

When reading the XML document, a mapping between the root node name, attributes, and nested nodes will be made for the bean, set methods, and nested beans, respectively. Empty constructors for each bean and set methods for each attribute (basic type or nested bean) must be provided to accomplish the proper mapping.

Figure 3 shows a ContactInfo XML document, a matching ContactInfo bean class, and the bean object resulting from the fromXML() method invocation.

Figure 3. fromXML() method invocation

Usage example

Let's examine an example of BeanXMLMapping usage. The sample consists of XML, JavaBean classes, and snapshots of an online banking application. You can download the example source code from Resources.

Figure 4 is a sample account history page in an online banking application.

Figure 4. Sample account history page

Figure 5 displays the AccountHistory bean class diagram, while Listing 3 shows an XML document generated through its toXML() method invocation.

Figure 5. AccountHistory bean class diagram. Click on thumbnail to view full-size image.

Listing 3. Account history XML representation

<?xml version="1.0" encoding="ISO-8859-1"?>
<AccountHistory>
  <transactionList>
    <transaction>
      <deposit></deposit>
      <withdraw>-,150.00 </withdraw>
      <date>3/10/2002 </date>
      <description>Check Number: 213 </description>
      <balance>,340.50</balance>
    </transaction>
    <transaction>
      <deposit></deposit>
      <withdraw>-51.50</withdraw>
      <date>3/7/2002</date>
      <description>ATM Withdrawal 350 SAN JOSE CA</description>
      <balance>,189.00</balance>
    </transaction>
    <transaction>
      <deposit>,060.40 </deposit>
      <withdraw></withdraw>
      <date>3/5/2002</date>
      <description>ACH DEPOSIT - MyCompany </description>
      <balance>,249.40</balance>
    </transaction>
    <transaction>
      <deposit></deposit>
      <withdraw>-0.00</withdraw>
      <date>2/26/2002</date>
      <description>JC'S BBQ AND DELI SAN JOSE CA </description>
      <balance>,229.40</balance>
    </transaction>
  </transactionList>
  <accountHistoryContext>
    <account>
      <holder>Paulo Caroli</holder>
      <type>checkings</type>
      <number>316614-10</number>
    </account>
    <dateTo>Monday, March 11, 2002 </dateTo>
    <dateFrom>Monday, February 25, 2002 </dateFrom>
  </accountHistoryContext>
</AccountHistory>


Figure 6's sequence diagram shows a possible scenario where an AccountHistory bean object is reached and its toXML() method invoked.

Figure 6. Simplified sequence diagram for getting AccountHistory XML document. Click on thumbnail to view full-size image.

Figure 7 is a sample page for requesting account history in an online banking application. The bank account and dates are retrieved and displayed on this page; Listing 4 shows how such information can be represented as XML.

Figure 7. Sample page for requesting account history in an online banking application

Listing 4. Account history context XML representation

<?xml version="1.0" encoding="ISO-8859-1"?>
<AccountHistoryContext>
  <account>
    <holder>Paulo Caroli</holder>
    <type>checkings</type>
    <number>316614-10</number>
  </account>
  <dateTo>Monday, March 11, 2002 </dateTo>
  <dateFrom>Monday, February 25, 2002 </dateFrom>
</AccountHistoryContext>


AccountHistoryContext.FromXML() is invoked to retrieve the AccountHistoryContext bean object for the respective XML document.

Keep it simple!

Fortunately, it is this simple. Conversion from JavaBean to XML and vice versa happens smoothly, without requiring much from the JavaBean code. Developers can benefit from the use of the BeanXMLMapping component and code at a high level without dealing with any XML-specific library.

There's no need for complexity to accomplish JavaBean-XML mapping. Take advantage of the BeanXMLMapping component and enjoy your XML-empowered beans.

About the author

Paulo Caroli is a project manager at Omni Pros. Paulo has a master's degree in software engineering and is a Sun Certified Architect for Java Technology with more than nine years of experience in application development. He is an expert in object-oriented techniques who specializes in Java 2 Platform, Enterprise Edition (J2EE) Web application development.

Read more about Core Java in JavaWorld's Core Java section.

  • Print
  • Feedback

Resources