看板java
标 题[文件] 使用 Digester 读取 xml
发信站SayYA 资讯站 (Wed Jan 17 20:18:42 2007)
转信站ptt!ctu-reader!news.nctu!SayYa
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