ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

iBatis DAO
Pages: 1, 2, 3, 4, 5

Developing an XML Transaction Map

There may be situations when you want to read and write data from an XML file instead of an RDBMS. For example, imagine working on a banking project where you don't have direct access to bank's database, and instead the customer provides you with sample data in the form of XML files. Your requirement is that your application should use these XML files during development, and a RDBMS in production. We will take our contact application as example and change it so that it will read and write data from an XML file instead of an RDBMS. This requires two changes:

  1. Add support for an external transactionManager in your DAOMap.xml file.
  2. Create a XMLContactDAO.java file, as follows:
    public class XMLContactDAO implements ContactDAO {
     public static final String
      CONTACTXMLNAME = "c:\\Contact.xml";
     public XMLContactDAO(DaoManager manager) {
     public int insertContact(Contact contact) {
      HashMap contactMap = loadChanges();
      if (contactMap.get(new Integer
        (contact.getContactId())) == null)
        Integer(contact.getContactId()), contact);
      return contact.getContactId();
     public Contact selectContact(int contactId) {
      HashMap contactMap = loadChanges();
      return (Contact) contactMap.get(
       new Integer(contactId));
     public HashMap loadChanges() {
      HashMap contactMap = null;
      try {
       XStream xstream = new XStream(new DomDriver());
       xstream.alias("contact", Contact.class);
       contactMap =
        (HashMap) xstream.fromXML(
         new FileReader(CONTACTXMLNAME),HashMap.class);
      } catch (FileNotFoundException e) {
        return new HashMap();
      return contactMap;
     public void saveChanges(HashMap contactMap) {
      try {
       XStream xstream = new XStream();
       xstream.alias("contact", Contact.class);
        new FileWriter(CONTACTXMLNAME));
       } catch (IOException e) {

In this example, XMLContactDAO implements the ContactDAO business interface. Since we are using an EXTERNAL transaction manager, we cannot use any of the existing Template classes. In our class, we have created two simple methods--loadChanges() and saveChanges--for reading and writing an XML file using the XStream framework. XStream is open source framework that helps you read an XML file as a Java object and save a Java object as an XML file.


Nowadays, a lot of new persistence frameworks are coming up. As a developer, this is both good and bad for you. It's good because now you have many more options to choose from. But it's bad because you have to make choices. And the bigger problem is that you have to commit to one persistence framework at the start of the project, at which point you may not be completely clear on project requirements or completely sure if a particular framework can fulfill all your requirements. DAO is a very easy-to-use and useful framework that guards against changes in persistence mechanisms. It may look like you're making some investment up front, but it will definitely help you in the long run.


Sunil Patil has worked on J2EE technologies for more than five years. His areas of interest include object relational mapping tools, UI frameworks, and portals.

Return to ONJava.com.