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

Advertisements

8 Comments »

  1. OMG! It is too deep! kill me~

    Comment by Yuan — October 10, 2007 @ 6:34 am |Reply

  2. OMG! It is too deep!
    Kill me~

    Comment by Yuan — October 10, 2007 @ 6:34 am |Reply

  3. I even don’t know what is the diffierence between xml and html, and only know xml is stricter than html. Recently, I am doing some research on HSRP and VRRP. If you know something about that, please kindly tell us.Haha~

    Comment by Yuan — October 10, 2007 @ 7:08 am |Reply

  4. Hei there, nice post. Congratulations.

    Comment by Finalfire — December 15, 2007 @ 4:28 pm |Reply

  5. Hi, that’s a usefull post.

    by the way, do you know how to create an xml file that display button, radiobutton, scrollbar, and textarea?

    thanks. vivi

    Comment by vivi — January 29, 2008 @ 5:14 pm |Reply

  6. awesome… thanks for your info. care to visit my site at http://the-soa.blogspot.com

    Comment by the-SOA — August 26, 2008 @ 1:03 pm |Reply

  7. Have you looked at VTD-XML? it is 10x faster and 5x more memory efficent tha DOM4J or JDOM

    Comment by anon — November 23, 2009 @ 6:09 pm |Reply

  8. I liked the above girl’s snap

    Comment by test user — May 7, 2010 @ 7:15 am |Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: