learning and using jakarta digester
10/23/2002 【程序编程相关:MVC循序渐进(环境篇) eclipse】by philipp k. janert, ph.d. 【推荐阅读:Eclipse下集成OpenOffice】described how to accomplish this using the standard sax and dom apis. 【扩展信息:MVC循序渐进(jsp model1实现】
turning an xml document into a corresponding hierarchy of java bean objects is a fairly common task. in a previous article, ialthough powerful and flexible, both apis are, in effect, too low-level for the specific task at hand. furthermore, the
unmarshalling procedure itself requires a fair amount of coding: a parse-stack must be maintained when using sax, and the
dom-tree must be navigated when using dom.
this is where the apache jakarta commons digester framework comes in.
the jakarta digester framework
the jakarta digester framework grew out of the jakarta struts web toolkit. originally developed to process the centralstruts-config.xml configuration file, it was soon recognized that the framework was more generally useful, and moved to the
jakarta commons project, the stated goal of which is to provide a "repository of reusable java components." the most recent
version, digester 1.3, was released on august 13, 2002.
the digester class lets the application programmer specify a set of actions to be performed whenever the parser encounters
certain simple patterns in the xml document. the digester framework comes with 10 prepackaged "rules," which cover most of
the required tasks when unmarshalling xml (such as creating a bean or setting a bean property), but each user is free to
define and implement his or her own rules, as necessary.
the example document and beans
in this example, we will unmarshall the same xml document that we used in the previous article:<?xml version="1.0"?>
<catalog library="somewhere">
<book>
<author>author 1</author> <title>title 1</title> </book><book>
<author>author 2</author> <title>his one book</title> </book><magazine>
<name>mag title 1</name><article page="5">
<headline>some headline</headline> </article><article page="9">
<headline>another headline</headline> </article> </magazine><book>
<author>author 2</author> <title>his other book</title> </book><magazine>
<name>mag title 2</name>... 下一页