Jakarta Commons——Digester

主要用于由xml生成java对象。基本原理是:使用pattern匹配特定的节点,构造对象,并执行rule定义的操作。

Pattern使用xpath匹配节点,是从根目录开始,只有一个例外:*/name will match a <name> element anywhere in the document.

使用步骤

1)创建digester的实例;2)config it ; 3) 指定pattern和rule; 4)选择xml文件;5)得到解析结果

import org.apache.commons.digester.*;

import java.io.*;
import java.util.*;

public class DigesterDriver {

   public static void main( String[] args ) {

      try {
         Digester digester = new Digester();
         digester.setValidating( false );   // 是否使用dtd 验证xml

         digester.addObjectCreate( "catalog", Catalog.class );   //创建ObjectCreateRule,并放入解析堆栈

         digester.addObjectCreate( "catalog/book", Book.class );
         digester.addBeanPropertySetter( "catalog/book/author", "author" );   //设置bean.author为对应的 xml的 book/author节点
         digester.addBeanPropertySetter( "catalog/book/title", "title" );
         digester.addSetNext( "catalog/book", "addBook" );

         digester.addObjectCreate( "catalog/magazine", Magazine.class );
         digester.addBeanPropertySetter( "catalog/magazine/name", "name" );

         digester.addObjectCreate( "catalog/magazine/article", Article.class );
         digester.addSetProperties( "catalog/magazine/article", "page", "page" );
         digester.addBeanPropertySetter( "catalog/magazine/article/headline" ); 
         digester.addSetNext( "catalog/magazine/article", "addArticle" );

         digester.addSetNext( "catalog/magazine", "addMagazine" );

         File input = new File( args[0] );
         Catalog c = (Catalog)digester.parse( input );

         System.out.println( c.toString() );

      } catch( Exception exc ) {
         exc.printStackTrace();
      }
   }
}

         如果为一个Pattern创建多个rule,则使用 创建的顺序来进行解析。 to deal with the <article> element, found at catalog/magazine/article, we first create the appropriate article bean, then set the page property, and finally pop the completed article bean and insert it into its magazine parent.

调用任意方法

          不只是能设置bean的属性,还可以调用bean的任意方法,如:

digester.addCallMethod( "catalog/book/author", "setAuthor", 1 );   // 调用 setAuthor,且参数个数为1
digester.addCallParam( "catalog/book/author", 0 );         // 参数为 author 节点的值


 

标准的rule

     1) creation: objectCreateRule(根据pattern和bean class创建bean,并放入stack top), FactoryCreateRule ( 使用工厂类创建bean,并放入stack top.)

     2)property setter:  SetPropertiesRule ( 使用attribute设置bean的property) BeanPropertySetterRule(使用element和text设置bean的Property)。SetPropertyRule(忽略)

     3)parent and child setNextRule(弹出栈顶元素,并调用第二个元素的指定方法插入,参数是栈顶元素, 常用于插入bean到父对象)SetTopRule(Passes the second-to-top object on the stack to the top-level object. This is useful if the child object exposes a setParent method, rather than the other way around.) SetRootRule:( Calls a method on the object at the bottom of the stack, passing the object on top of the stack as argument.)

     4)函数调用 CallMethodRule (调用栈顶元素的任意方法)    CallParamRule(传回CallMethodRule需要的函数参数)

使用xml文件声明pattern和rule    

    1)创建rule.xml文件。

    2)使用 org.apache.commons.digester.xmlrules .DigesterLoader.createDigester来创建Digester,必须遵守 digester-rules.dtd   

    3) 解析XML文件得到类。

实例:

<?xml version="1.0"?>

<digester-rules>
   <object-create-rule pattern="catalog" classname="Catalog" />
   <set-properties-rule pattern="catalog" >
      <alias attr-name="library" prop-name="library" />
   </set-properties-rule>

   <pattern value="catalog/book">
      <object-create-rule classname="Book" />
      <call-method-rule pattern="author" methodname="setAuthor"
	                paramcount="0" />
      <call-method-rule pattern="title" methodname="setTitle" 
	                paramcount="0" />
      <set-next-rule methodname="addBook" />
   </pattern>

   <pattern value="catalog/magazine">
      <object-create-rule classname="Magazine" />

      <call-method-rule pattern="name" methodname="setName" paramcount="0" />

      <pattern value="article">
         <object-create-rule classname="Article" />
         <set-properties-rule>
            <alias attr-name="page" prop-name="page" />
         </set-properties-rule>    
         <call-method-rule pattern="headline" methodname="setHeadline" 
		           paramcount="0" />
         <set-next-rule methodname="addArticle" />
      </pattern>

      <set-next-rule methodname="addMagazine" /> 
   </pattern>
</digester-rules>
        

       Patterns can be specified in two different ways: either as attributes to each XML element representing a rule, or using the <pattern> element. The pattern defined by the latter is valid for all contained rule elements. Both ways can be mixed, and <pattern> elements can be nested -- in either case, the pattern defined by the child element is appended to the pattern defined in the enclosing <pattern> element.

       The <alias> element is used with the <set-properties-rule> to map an XML attribute to a bean property.

       Finally, using the current release of the Digester package, it is not possible to specify the BeanPropertySetterRule in the configuration file. Instead, we are using the CallMethodRule to achieve the same effect, as explained above.

 

读取要解析的xml和rule.xml

import org.apache.commons.digester.*;
import org.apache.commons.digester.xmlrules.*;

import java.io.*;
import java.util.*;

public class XmlRulesDriver {
   public static void main( String[] args ) {
      try {

         File input = new File( args[0] );
         File rules = new File( args[1] );

         Digester digester = DigesterLoader.createDigester( rules.toURL() );

         Catalog catalog = (Catalog)digester.parse( input );
         System.out.println( catalog.toString() );
  
      } catch( Exception exc ) {
         exc.printStackTrace();
      }
   }
}


   有专门的Digester处理RSS (Rich-Site-Summary)-formatted newsfeeds

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值