org.apache.commons.digester 学习

org.apache.commons.digester软件包。以下网址有一个很不错的介绍:
[@more@]http://www.onjava.com/pub/a/onjava/2002/10/23/digester.html?page=1

它能方便地将XML文档所定义的元素转化为JAVA对象


  //生成一个digester。主要需要引进commons-logging.jar、commons-collections.jar、commons-      beanutils.jar 
         Digester digester = new Digester(); 
         
         //设置对XML文档资料是否进行DTD验证 
         digester.setValidating( false ); 
         
         //当遇见 catalog 元素的时候,产生一个Catalog对象 
         digester.addObjectCreate( "catalog", Catalog.class ); 
         
         //当遇见 catalog 元素下面的book的时候,产生一个Book对象 
         digester.addObjectCreate( "catalog/book", Book.class ); 
         // 当遇见 catalog 元素下面的book的author时候,调用author属性的Set方法 
         digester.addBeanPropertySetter( "catalog/book/author", "author" ); 
         digester.addBeanPropertySetter( "catalog/book/title", "title" ); 
         //当再一次遇见 catalog 元素下面的book的时候,调用catalog类的addBook()方法 
         digester.addSetNext( "catalog/book", "addBook" ); 

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

         digester.addObjectCreate( "catalog/magazine/article", Article.class ); 
         //addSetProperties()是将对应元素的属性赋值。 
         digester.addSetProperties( "catalog/magazine/article", "page", "page" ); 
         digester.addBeanPropertySetter( "catalog/magazine/article/headline" ); 
         digester.addSetNext( "catalog/magazine/article", "addArticle" ); 

         digester.addSetNext( "catalog/magazine", "addMagazine" ); 
         //"F:Digestercatalog.xml"为XML文档 
         File input = new File( "F:Digestercatalog.xml" ); 
         Catalog c = (Catalog)digester.parse( input ); 
         System.out.println( c.toString() ); 

你可能感兴趣的:(apache,C++,c,xml,C#)