digester解析器解析xml(java)

rule.xml的DTD    <!DOCTYPE digester-rules PUBLIC "-//Jakarta Apache//DTD digester-rules XML V1.0//EN" "http://jakarta.apache.org/commons/digester/dtds/digester-rules.dtd">

例如要解析

<class>
                  <student id="1" name="zhangsan">
                  </student>
                  <student id="2" name="lisi">
                 </student>
            </class>

这个xml文件

第一步:要确定导入相关的jar包

第二步:要确定rule.xml(这个xml名字可以随便起)。但是建议这么命名。

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE digester-rules PUBLIC "-//Jakarta Apache//DTD digester-rules XML V1.0//EN" "http://jakarta.apache.org/commons/digester/dtds/digester-rules.dtd">
<digester-rules>
 <pattern value="class">//value是上面的<class>的值
  <pattern value="student">//value是上面的<student>的值
   <object-create-rule
    classname="config.StudentBean" />//把上面的<student>中有的属性作为StudentBean类的属性写一个类,并附上get set方法
    <set-next-rule methodname="setStudebtBean"/>//在上层class中也该定义一个类,来存放student 那么setStudebtBean

该方法是写在classBean类中注入studentBean的时候的一个方法
    <set-properties-rule />//属性对应关系,如果不写 默认就是名字应该相同
  </pattern>
 </pattern>
</digester-rules>

 

两个类如下

studentBean类

package config;

                public class StudentBean {
                      private String id;
                      private String name;
                      public String getId() {
                         return id;
                 }
                 public void setId(String id) {
                    this.id = id;
                  }
                 public String getName() {
                  return name;
                 }
                 public void setName(String name) {
                   this.name = name;
                 }
                @Override
               public String toString(){
                    return "id:"+id+"and name:"+name;
                }
            }

ClassBean

package config;

                       import java.util.HashMap;
                       import java.util.Map;

                       public class ClassBean {
                                private Map<String,StudentBean> students= new HashMap<String,StudentBean>();

                                public StudentBean getStudentBean(String id){
                                        return students.get(id);
                                  }
 
                               public void setStudebtBean(StudentBean stu){
                                     students.put(stu.getId(), stu);
                                 }

                             @Override
                               public String toString() {
                                       return students.toString();
                                  } 
                                }

 

那么在servlet中进行解析

public void init()throws ServletException {
                     System.out.println("------init...--------");
                     Digester digester =DigesterLoader.createDigester(this.getClass().
                     getClassLoader().getResource("config/rule.xml"));//根据解析xml rule 来得到解析器
                     digester.push(cb);把对象推入解析器
                     //System.out.println(digester);
                     try {
                                 // System.out.println(this.getClass().getClassLoader().getResource("class.xml"));
                                    digester.parse(this.getClass().getClassLoader().getResource("class.xml"));
                                   //System.out.println(cb);
                    } catch (Exception e) {
                                 e.printStackTrace();
                    }     

                    }
  在web.xml中注意配置<load-on-startup>0</load-on-startup>这项配置 配置的数据越小优先级越高

   在完成上面一步之后,那么我们在每次启动服务器之时 会自动实例化 servlet 调用 init方法进行digester的解析

在理解servlet1的源代码 那么这是一个难点

你可能感兴趣的:(java,Digester,解析xml)