Digester

从XML到Java对象
在Java项目中经常使用XML配置文件管理后台应用规则、实现程序初始化等操作。Digester便是专门用于从XML映射到Java对象的开源组件。它最初起源于大名鼎鼎的Struts,用于其中的配置解析工作。后来考虑到其实用性,便将其提取为独立的Apache项目,即Digester。



示例1

有如下XML(config.xml):

<root>

      <parent pname="p1">

            <child cname="c1">child1</child>

            <child cname="c2">child2</child>

      </parent>

</root>


将其转化为如下对象:

Parent.java
Child.java

package com.vc.digester;



import java.util.ArrayList;

import java.util.List;



public class Parent {

       private String name;

       private List<Child> childs;



       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       public List<Child> getChilds() {

              return childs;

       }

       public void setChilds(List<Child> childs) {

              this.childs = childs;

       }

       public void addChild(Child c){

              if(childs==null)childs=new ArrayList<Child>();

              childs.add(c);

       }

}


package com.vc.digester;



public class Child {

       private String name;

       private String value;

      

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       public String getValue() {

              return value;

       }

       public void setValue(String value) {

              this.value = value;

       }

}








解析方法1:

TestConfig1.java

package com.vc.digester;



import java.util.ArrayList;

import java.util.List;

import org.apache.commons.digester.Digester;



public class TestConfig1 {

      

       /**

        * @param args

        */

       public static void main(String[] args) {

              Digester digester=new Digester();

              //digester.setValidating(false);

              digester.addObjectCreate("root",ArrayList.class);//create an ArrayList when got a ‘/root’ element

              digester.addObjectCreate("root/parent", Parent.class);

              digester.addObjectCreate("root/parent/child", Child.class);

             

              digester.addSetProperties("root/parent", "pname","name");

              digester.addSetProperties("root/parent/child","cname","name");//set propertiy:cname to Child.name

              digester.addBeanPropertySetter("root/parent/child","value");//set node value to Child.value

             

              digester.addSetNext("root/parent", "add");//use add() method of List

              digester.addSetNext("root/parent/child","addChild");

             

              try {

                     List<Parent> parents=(ArrayList<Parent>) digester.parse("C:/ config.xml");

                     System.out.println(parents.get(0));

              } catch (Exception e) {

                     e.printStackTrace();

              }

       }



}








附:

HomePage:http://commons.apache.org/digester/

Download src and binaries:http://commons.apache.org/digester/download_digester.cgi

你可能感兴趣的:(java)