<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Five 's Weblog &#187; Dom4j</title>
	<atom:link href="http://powerdream5.wordpress.com/tag/dom4j/feed/" rel="self" type="application/rss+xml" />
	<link>http://powerdream5.wordpress.com</link>
	<description>It is not just another blog! 伍行天下！</description>
	<lastBuildDate>Wed, 28 Nov 2007 21:21:29 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='powerdream5.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/b83f3cb0955eab114902a009525db137?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Five 's Weblog &#187; Dom4j</title>
		<link>http://powerdream5.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://powerdream5.wordpress.com/osd.xml" title="Five &#8217;s Weblog" />
		<item>
		<title>An introductin to Dom4j!</title>
		<link>http://powerdream5.wordpress.com/2007/10/10/an-introductin-to-dom4j/</link>
		<comments>http://powerdream5.wordpress.com/2007/10/10/an-introductin-to-dom4j/#comments</comments>
		<pubDate>Wed, 10 Oct 2007 00:52:39 +0000</pubDate>
		<dc:creator>powerdream5</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Dom4j]]></category>

		<guid isPermaLink="false">http://powerdream5.wordpress.com/2007/10/10/an-introductin-to-dom4j/</guid>
		<description><![CDATA[        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 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=powerdream5.wordpress.com&blog=1840509&post=34&subd=powerdream5&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p align="left"><a href="http://powerdream5.files.wordpress.com/2007/10/9_1_2007.JPG" title="9_1_2007.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/9_2_2007.jpg" title="9_2_2007.jpg"></a>        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.<br />
        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 <strong><a target="_blank" href="http://www.dom4j.org/">Dom4j</a>, an open source framework for handling xml files on the Java platform</strong>. At the front beginning, you need to download Dom4j, and add the dom4j.jar package into your classpath.</p>
<p>        Now, I presume there is a xml file(books.xml), as the following shows:<br />
<font color="#ff6600">&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;gb2312&#8243;?&gt;<br />
&lt;books&gt;<br />
      &lt;book&gt;<br />
           &lt;title category=&#8221;J2EE&#8221;&gt;Enterprise Application Architecture&lt;/title&gt;<br />
           &lt;author&gt;Martin Flower&lt;/author&gt;<br />
      &lt;/book&gt;<br />
      &lt;book&gt;<br />
           &lt;title category=&#8221;Software Engineering&#8221;&gt;Domain Driven Design&lt;/title&gt;<br />
           &lt;author&gt;Eric Evans&lt;/author&gt;<br />
      &lt;/book&gt;<br />
&lt;/books&gt;</font></p>
<p><font color="#000000">       The progrm used to parse this xml file(TestDom4j.java) is:</font></p>
<p><font color="#000000">import java.io.File;<br />
import java.util.Iterator;</font><br />
<font color="#ff0000">//import the class from the dom4j.jar package</font><br />
<font color="#000000">import org.dom4j.Document;<br />
import org.dom4j.DocumentException;<br />
import org.dom4j.io.SAXReader;<br />
import org.dom4j.Element;<br />
import org.dom4j.Attribute;<br />
import org.dom4j.tree.DefaultElement;</font></p>
<p><font color="#000000">public class TestDom4j{</font></p>
<p><font color="#000000">     public Document parse(File file) throws DocumentException{</font><br />
           <font color="#ff0000">//create document object for the xml file, then we can navigate the document object</font><br />
<font color="#000000">           SAXReader reader = new SAXReader();<br />
           Document document = reader.read(file);<br />
           return document;<br />
 }</font></p>
<p><font color="#000000"> public void display(Element root) throws DocumentException{</font><br />
<font color="#000000">        <font color="#ff0000">//using Iterator to output each child of the root element<br />
</font>        for(Iterator i=root.elementIterator();i.hasNext();){</font><font color="#000000"><br />
               Element element = (Element)i.next();<br />
               System.out.println(&#8220;\t&lt;&#8221;+element.getName()+&#8221;&gt;&#8221;);<br />
               </font><font color="#ff0000">//List all the children of a element, and change the list into an array<br />
</font><font color="#000000">               List children = element.elements();<br />
               Object[] child = children.toArray();<br />
               for(int k=0; k&lt;child.length;k++)<br />
             {<br />
                    DefaultElement de= (DefaultElement)child[k];<br />
                    System.out.print(&#8220;\t\t&lt;&#8221;+de.getName());<br />
                    <font color="#ff0000">//get the attribute of category of a child</font></font><br />
<font color="#000000">                    Attribute category = de.attribute(&#8220;category&#8221;);<br />
                   <font color="#ff0000">// if a child has the attribute of category, print it out</font></font><br />
<font color="#000000">                   if(category != null)<br />
                   {<br />
                          System.out.print(&#8221; category=\&#8221;"+category.getValue()+&#8221;\&#8221;&gt;&#8221;);<br />
                    }else{<br />
                          System.out.print(&#8220;&gt;&#8221;);<br />
                    }<br />
                    System.out.print(de.getText());<br />
                    System.out.println(&#8220;&lt;/&#8221;+de.getName()+&#8221;&gt;&#8221;);<br />
               }<br />
               System.out.println(&#8220;\t&lt;/&#8221;+element.getName()+&#8221;&gt;&#8221;);<br />
       }<br />
 }</font></p>
<p><font color="#000000">       public static void main(String[] args) throws Exception {<br />
              File file = new File(&#8220;books.xml&#8221;);<br />
              TestDom4j t = new TestDom4j();</font><br />
<font color="#000000">              <font color="#ff0000">//get the document object of the xml file<br />
</font>              Document document = t.parse(file);<br />
             <font color="#ff0000">//get the root element of the xml file, and print it out<br />
</font>              Element element = document.getRootElement();<br />
              System.out.println(&#8220;&lt;&#8221;+element.getName()+&#8221;&gt;&#8221;);<br />
             <font color="#ff0000">//Beginning from the root element, naviagte the xml file</font><br />
              t.display(element);<br />
              System.out.println(&#8220;&lt;/&#8221;+element.getName()+&#8221;&gt;&#8221;);<br />
       }<br />
}</font></p>
<p>The running result of this program is:</p>
<p><a href="http://powerdream5.files.wordpress.com/2007/10/9_1_2007.JPG" title="9_1_2007.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/9_1_2007.JPG" title="9_1_2007.JPG"></a><a href="http://powerdream5.files.wordpress.com/2007/10/9_1_2007.JPG" title="9_1_2007.JPG"></a></p>
<p style="text-align:center;"><img width="456" src="http://powerdream5.files.wordpress.com/2007/10/9_1_2007.JPG?w=456&#038;h=235" alt="9_1_2007.JPG" height="235" style="width:503px;height:274px;" /></p>
<p>         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.</p>
<p>        At last, I have to say, to be honest, I don&#8217;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.</p>
<p><a href="http://powerdream5.files.wordpress.com/2007/10/9_2_2007.jpg" title="9_2_2007.jpg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/9_2_2007.jpg" title="9_2_2007.jpg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/9_2_2007.jpg" title="9_2_2007.jpg"></a><a href="http://powerdream5.files.wordpress.com/2007/10/9_2_2007.jpg" title="9_2_2007.jpg"></p>
<p style="text-align:center;"><img src="http://powerdream5.files.wordpress.com/2007/10/9_2_2007.jpg" alt="9_2_2007.jpg" /></p>
<p></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/powerdream5.wordpress.com/34/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/powerdream5.wordpress.com/34/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/powerdream5.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/powerdream5.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/powerdream5.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/powerdream5.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/powerdream5.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/powerdream5.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/powerdream5.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/powerdream5.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/powerdream5.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/powerdream5.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=powerdream5.wordpress.com&blog=1840509&post=34&subd=powerdream5&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://powerdream5.wordpress.com/2007/10/10/an-introductin-to-dom4j/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a0819a06b6ff4afc4f3e0a1977e6a5a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">powerdream5</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/9_1_2007.JPG" medium="image">
			<media:title type="html">9_1_2007.JPG</media:title>
		</media:content>

		<media:content url="http://powerdream5.files.wordpress.com/2007/10/9_2_2007.jpg" medium="image">
			<media:title type="html">9_2_2007.jpg</media:title>
		</media:content>
	</item>
	</channel>
</rss>