http://jakarta.apache.org/commons/digester/
[功能]
讀取 xml 資料
[下載與安裝]
使用版本 Digester 1.8
相依 component
Logging 1.1.x
BeanUtils 1.x
Collections (2.x/3.x)
安裝方式: 同一般的 classpath 設定方式 Smile
[一些基本概念]
1. xpath 相似的 pattern 觀
即以 xml root element 與其 child element 簡單的以 root/child_A/child_B 去
match 到所需的 element
2. 以 java bean 去做 java object-xml mapping
以 yahoo 即時新聞的 rss feed 為例,
http://tw.news.yahoo.com/rss/realtime
我們可以將他視為是一個 News 類別,
News 類別中含有很多個 Item 類別實體,每個 Item 就是新聞短訊。
=======================================================================
[刻 data object]
import java.util.Vector;
/**
* News store all the news item
*
* @author qrtt1
*
*/
public class News {
// use vector hold items
private Vector<Item> items;
public News() {
items = new Vector<Item>();
}
/**
* @param i
* add a new item
*/
public void addItem(Item i) {
items.add(i);
}
/**
* @return all the items
*/
public Vector<Item> getItems() {
return items;
}
}
/**
* Item class hold a news entry
*
* @author qrtt1
*
*/
public class Item {
private String title;
private String link;
/**
* @return item's link
*/
public String getLink() {
return this.link;
}
/**
* @param link
* item's link
*/
public void setLink(String link) {
this.link = link;
}
/**
* @return item's title
*/
public String getTitle() {
return this.title;
}
/**
* @param title
* item's title
*/
public void setTitle(String title) {
this.title = title;
}
/*
* @see java.lang.Object#toString()
*/
public String toString() {
return "Item [title=" + title + ",link=" + link + "]";
}
}
寫完了基本的 data object 後,接下來我們要來處理 digester 了
你會發現和 dom 或相類的 parser 用法完全不同的感覺
===============================================================
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;
import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;
/**
* Digester Test Application, this sample code demo how to use Digester
*
* @author qrtt1
*
*/
public class DigesterTest {
public static void main(String[] args) throws IOException, SAXException {
// get an instance for digester
Digester digester = new Digester();
// create News bean while the "rss/channel" xpath pattern match
digester.addObjectCreate("rss/channel", News.class);
// create Item bean while the "rss/channel/item" xpath pattern match
digester.addObjectCreate("rss/channel/item", Item.class);
// use the bean setter method
digester.addBeanPropertySetter("rss/channel/item/title", "title");
digester.addBeanPropertySetter("rss/channel/item/link", "link");
// if the pattern "rss/channel/item" match aply the next rule
digester.addSetNext("rss/channel/item", "addItem");
// parse the xml
News n = (News) digester.parse("http://tw.news.yahoo.com/rss/realtime");
// show the result
Vector<Item> v = n.getItems();
Iterator it = v.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
=====================================================================
好唄,其實你會發現其中的奧妙。只寫了 2 個 java bean 和一個主程式
你要的資料都讀進來了。
<%
Item [title=卡崔娜颶風倖存胚胎誕生 (民視
),link=http://tw.rd.yahoo.com/referurl/news/rss/realtime/*http://tw.news.yahoo.com/article/url/d/a/070117/11/9grl.html]
Item [title=2011世大運 中國深圳主辦 (民視
),link=http://tw.rd.yahoo.com/referurl/news/rss/realtime/*http://tw.news.yahoo.com/article/url/d/a/070117/11/9grk.html]
I
..................................
%>
[參考資料]
Learning and Using Jakarta Digester
http://www.onjava.com/pub/a/onjava/2002/10/23/digester.html
Simplify XML file processing with the Jakarta Commons Digester
http://www.javaworld.com/javaworld/jw-10-2002/
jw-1025-opensourceprofile_p.html
--
web 版:
http://www.javaworld.com.tw/jute/post/view?bid=11&id=180873&sty=1&tpg=1&age=0
--
※ Origin: SayYA 資訊站 <bbs.sayya.org>
◆ From: 163.26.34.247