<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    junhong

    Simplify XML file processing with the Jakarta Commons Digester

    the Apache Jakarta site is home to many well-known Java open source projects, including Tomcat, Ant, and log4j. A lesser-known subproject of Jakarta is Jakarta Commons, a repository of reusable Java components. These components, such as Commons BeanUtils, Commons DBCP, and Commons Logging, alleviate the pain of some standard programming tasks. This article will focus on the Jakarta Commons Digester, a utility that maps XML files to Java objects.

    Note: To use Digester, you must have the following libraries in your classpath: Digester, BeanUtils, Collections, Logging, and an XML parser conforming to SAX (Simple API for XML) 2.0 or JAXP (Java API for XML Parsing) 1.1. Links to all Jakarta Commons components, along with two suitable parsers, Crimson and Xerces, can be found in Resources.

    XML parsing overview
    Two basic methods parse XML documents. One is the Document Object Model (DOM) method. When parsing an XML document with DOM, the parser reads the entire document and creates a tree-like representation of it. The second method uses SAX and parses XML documents with events. The DOM method, while sometimes easier to implement, is slower and more resource-intensive than SAX. Digester simplifies SAX parsing by providing a higher-level interface to SAX events. This interface hides much of the complexity involved in XML document navigation, allowing developers to concentrate on processing XML data instead of parsing it.

    Digester concepts
    Digester introduces three important concepts: element matching patterns, processing rules, and the object stack.

    Element matching patterns associate XML elements with processing rules. The following example shows the element matching patterns for an XML hierarchy:

    <datasources>??????????'datasources'
    ??<datasource>???????? 'datasources/datasource'
    ????<name/>????????????'datasources/datasource/name'
    ????<driver/>??????????'datasources/datasource/driver'??
    ??</datasource>
    ??<datasource>???????? 'datasources/datasource'
    ????<name/>????????????'datasources/datasource/name'
    ????<driver/>??????????'datasources/datasource/driver'??
    ??</datasource>
    </datasources>

    Each time a pattern is matched, an associated rule is fired. In the above example, a rule associated with 'datasources/datasource' executes twice.

    Processing rules define what happens when Digester matches a pattern. Digester includes predefined processing rules. Custom rules can also be created by subclassing org.apache.commons.digester.Rule.

    The object stack makes objects available for manipulation by processing rules. Objects can be added or removed (pushed or popped) from the stack either manually or through processing rules.

    Using Digester
    Digester is often used to parse XML configuration files. In the following examples, we have an XML configuration file that contains information used to build a DataSources pool. The DataSource is a hypothetical class that has an empty constructor and many get and set methods that take in and return strings.

    <?xml version="1.0"?>
    <datasources>
    ??<datasource>
    ????<name>HsqlDataSource</name>
    ????<driver>org.hsqldb.jdbcDriver</driver>
    ????<url>jdbc:hsqldb:hsql://localhost</url>
    ????<username>sa</username>
    ????<password></password>
    ??</datasource>
    ??<datasource>
    ????<name>OracleDataSource</name>
    ????<driver>oracle.jdbc.driver.OracleDriver</driver>
    ????<url>jdbc:oracle:thin:@localhost:1521:orcl</url>
    ????<username>scott</username>
    ????<password>tiger</password>
    ??</datasource>
    </datasources>

    To use Digester you must create an instance of the Digester class, push any required objects to the Digester's object stack, add a set of processing rules, and finally parse the file. Here is an example:

    Digester digester = new Digester();
    ????
    digester.addObjectCreate("datasources/datasource", "DataSource");
    digester.addCallMethod("datasources/datasource/name","setName",0);
    digester.addCallMethod("datasources/datasource/driver","setDriver", 0);

    digester.parse("datasource.xml");

    In this example, the addObjectCreate() method adds an ObjectCreateRule to the 'datasources/datasource' pattern. The ObjectCreateRule creates a new instance of the DataSource class and pushes the instance to the Digester's object stack. Next, addCallMethod() adds a CallMethodRule to two patterns. The CallMethodRule calls the specified method of the object at the top of the object stack. The last addCallMethod() argument is the number of additional parameters to be passed into the method. Since the number is zero, the matching element's body passes to the method.

    If this code runs against our sample XML file, here's what happens:

    • A new instance of the DataSource class is created and pushed to the object stack
    • The setName(String name) method of the newly instantiated DataSource object is called with the argument 'HsqlDataSource'
    • The setDriver(String driver) method of the newly instantiated DataSource object is called with the argument 'OracleDataSource'
    • At the end of the 'datasource' element, the object pops off the stack, and the process repeats itself

    The problem with this example is that the ObjectCreateRule pops off the object it creates when its associated element completes. When Digester finishes parsing the document, only the last object created remains. Solve this problem by pushing an object to the stack before parsing begins, and then call that object's methods to create any objects you need. The following class provides an example of this:

    public class SampleDigester
    {
    ??public void run() throws IOException, SAXException
    ??{????
    ????Digester digester = new Digester();

    ????// This method pushes this (SampleDigester) class to the Digesters
    ????// object stack making its methods available to processing rules.
    ????digester.push(this);

    ????// This set of rules calls the addDataSource method and passes
    ????// in five parameters to the method.
    ????digester.addCallMethod("datasources/datasource", "addDataSource", 5);
    ????digester.addCallParam("datasources/datasource/name", 0);
    ????digester.addCallParam("datasources/datasource/driver", 1);
    ????digester.addCallParam("datasources/datasource/url", 2);
    ????digester.addCallParam("datasources/datasource/username", 3);
    ????digester.addCallParam("datasources/datasource/password", 4);

    ????
    ????// This method starts the parsing of the document.
    ????digester.parse("datasource.xml");
    ??}

    ??// Example method called by Digester.
    ??public void addDataSource(String name,
    ????????????????????????????String driver,
    ????????????????????????????String url,
    ????????????????????????????String userName,
    ????????????????????????????String password)
    ??{
    ????// create DataSource and add to collection...
    ??}
    }

    In the SampleDigester class, the addDataSource() method is called each time the pattern 'datasources/datasource' is matched. The addCallParam() methods add CallParamRules that pass the matching elements' bodies as addDataSource() method parameters. In the addDataSource() method, you create the actual DataSource and add it to your collection of DataSources.

    Digest the Digester
    Although Digester was initially developed to simplify XML-configuration file parsing, it is useful any time you need to map XML files to Java objects. This article has provided an introduction to Digester. To learn more about Digester and other Jakarta Commons components, visit the Jakarta Commons Website. In addition, look at the open source projects in the Resources section below for real-world examples of Digester in action. You can also download the source code that accompanies this article below.

    posted on 2006-03-21 18:21 junhong 閱讀(259) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 黄色短视频免费看| 久久精品国产亚洲| 亚洲av永久无码一区二区三区| 99久热只有精品视频免费观看17| 亚洲无人区午夜福利码高清完整版| 国产福利电影一区二区三区,免费久久久久久久精 | 精品一区二区三区无码免费直播 | 亚洲AV无码一区二区三区牛牛| 亚洲一区二区三区免费在线观看| 亚洲伊人tv综合网色| 91人人区免费区人人| 亚洲精品一二三区| 免费日本黄色网址| 亚洲一级片免费看| 亚洲av无码一区二区三区乱子伦 | 亚洲国产高清国产拍精品| 国产精品色午夜免费视频| 曰批免费视频播放免费| 亚洲乱亚洲乱妇无码麻豆| 久久久精品免费视频| 亚洲一区免费视频| mm1313亚洲精品无码又大又粗| 一级毛片正片免费视频手机看| 亚洲不卡中文字幕无码| 永久免费的网站在线观看| 免费夜色污私人影院网站电影| 亚洲中文字幕久久精品无码APP | 亚洲av日韩综合一区久热| 国产成人亚洲影院在线观看| 久久成人免费播放网站| 伊人久久五月丁香综合中文亚洲 | 国产成人精品日本亚洲专一区| 国产福利免费在线观看| 国产一级一毛免费黄片| 亚洲伦理一二三四| 亚洲精品第一国产综合精品99| 99久热只有精品视频免费观看17| 麻豆亚洲AV成人无码久久精品 | 亚洲福利电影一区二区?| 国产免费黄色大片| 久久精品无码专区免费青青|