Five ‘s Weblog

October 10, 2007

An introductin to Dom4j!

Filed under: Programming — by powerdream5 @ 12:52 am
Tags:

        SOA(service oriented architecture) is a popular word in the enterprise application field. Service is defined by the messages exchanged among services. Up to now, most people admit that using web services to connect different parts of the application or different applications and using xml files to define the format of the messages are the best practices.
        Therefore, XML plays a significant role in building SOA. How to parse xml files will be one of the core problems we need to consider. Today, I want to make an introduction to Dom4j, an open source framework for handling xml files on the Java platform. At the front beginning, you need to download Dom4j, and add the dom4j.jar package into your classpath.

        Now, I presume there is a xml file(books.xml), as the following shows:
<?xml version=”1.0″ encoding=”gb2312″?>
<books>
      <book>
           <title category=”J2EE”>Enterprise Application Architecture</title>
           <author>Martin Flower</author>
      </book>
      <book>
           <title category=”Software Engineering”>Domain Driven Design</title>
           <author>Eric Evans</author>
      </book>
</books>

       The progrm used to parse this xml file(TestDom4j.java) is:

import java.io.File;
import java.util.Iterator;

//import the class from the dom4j.jar package
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.Element;
import org.dom4j.Attribute;
import org.dom4j.tree.DefaultElement;

public class TestDom4j{

     public Document parse(File file) throws DocumentException{
           //create document object for the xml file, then we can navigate the document object
           SAXReader reader = new SAXReader();
           Document document = reader.read(file);
           return document;
 }

 public void display(Element root) throws DocumentException{
        //using Iterator to output each child of the root element
        for(Iterator i=root.elementIterator();i.hasNext();){

               Element element = (Element)i.next();
               System.out.println(“\t<“+element.getName()+”>”);
              
//List all the children of a element, and change the list into an array
               List children = element.elements();
               Object[] child = children.toArray();
               for(int k=0; k<child.length;k++)
             {
                    DefaultElement de= (DefaultElement)child[k];
                    System.out.print(“\t\t<“+de.getName());
                    //get the attribute of category of a child

                    Attribute category = de.attribute(“category”);
                   // if a child has the attribute of category, print it out

                   if(category != null)
                   {
                          System.out.print(” category=\””+category.getValue()+”\”>”);
                    }else{
                          System.out.print(“>”);
                    }
                    System.out.print(de.getText());
                    System.out.println(“</”+de.getName()+”>”);
               }
               System.out.println(“\t</”+element.getName()+”>”);
       }
 }

       public static void main(String[] args) throws Exception {
              File file = new File(“books.xml”);
              TestDom4j t = new TestDom4j();

              //get the document object of the xml file
              Document document = t.parse(file);
             //get the root element of the xml file, and print it out
              Element element = document.getRootElement();
              System.out.println(“<“+element.getName()+”>”);
             //Beginning from the root element, naviagte the xml file
              t.display(element);
              System.out.println(“</”+element.getName()+”>”);
       }
}

The running result of this program is:

9_1_2007.JPG

         After reading the example, I hope you can have some basic ideas of using Dom4j. By the way, the example just demonstrates one way of handling xml files by dom4j. There are still a lot of other ways of using dom4j to parse xml files. You can refer to the official website of dom4j for more informaiton.

        At last, I have to say, to be honest, I don’t like parsing the xml files, because I have to make a specific program to parse a specific xml file each time, and this program cannot parse any other xml files. Therefore, I prefer to database to stroing data, However, sometimes, xml is our only choice.

9_2_2007.jpg

Blog at WordPress.com.