Digester 解析XMLITeye - 亚美娱乐

Digester 解析XMLITeye

2019年03月04日10时51分08秒 | 作者: 翠彤 | 标签: 元素,参数,解析 | 浏览: 1215

 

常规,供给参阅衔接, 高大全:http://www.massapi.com/class/di/Digester.html

                                          api:http://commons.apache.org/proper/commons-digester/commons-digester-3.0/apidocs/

 

1. 其完成思路是根据XML元素节点读取事情驱动的,依靠SAX。运用W3C 的XPATH来监听xml元素节点的读取。

 

2. 简略比如

   现有xml, test-members.xml:

 members 
 member name="Oham" species="human" 
 skill create /skill 
 equipment id="11" version="v1.0" Saurcer ship /equipment 
 equipment id="12" version="v1.2-beta" Predator mask /equipment 
 level 6 /level 
 /member 
 member name="Oham" species="dog" 
 skill know the truth /skill 
 skill free the soul /skill 
 level 12 /level 
 /member 
 /members 

  

  结构java bean,以映射xml,这儿结构两个bean,Members.java对应元素 members ;Member.java对应 member 。

 

Members.java:

package org.oham.xml;
import java.util.ArrayList;
import java.util.List;
public class Members {
 private List Member members = new ArrayList Member 
 public List Member getMembers() {
 return members;
 public void addMember(Member member) {
 members.add(member);

 

Member.java:

package org.oham.xml;
import java.util.HashSet;
import java.util.Set;
public class Member {
 private String name;
 private String species;
 private Set String skills = new HashSet String 
 private Set String equipments = new HashSet String 
 public String getName() {
 return name;
 public void setName(String name) {
 this.name = name;
 public String getSpecies() {
 return species;
 public void setSpecies(String species) {
 this.species = species;
 public Set String getSkills() {
 return skills;
 public Set String getEquipments() {
 return equipments;
 public void addSkill(String skill) {
 this.skills.add(skill);
 public void addEquipment(String equipment, int id, String version) {
 System.out.println("id: " + id + ", version: " + version + " stand by.");
 this.equipments.add(equipment);

  

 

运用Digester读取并解析xml,有几种种办法,介绍两种:

//运用办法一,调用Digester中的办法解析XPATH,
 public Members parse(File xmlFile) throws IOException, 
 SAXException {
 System.out.println("parse XPATH with API method");
 Digester digester = new Digester();
 // 遇到members元素节点开始时,结构Members类的目标
 digester.addObjectCreate("members", Members.class);
 // 遇到members元素的子元素member开始时,结构Member类的目标
 digester.addObjectCreate("members/member", Member.class);
 // set up members元素的子元素member的特点值, 条件是xml中的特点名有必要与java bean中的共同
 // 而且java bean 要有对应的setter办法
 digester.addSetProperties("members/member");
 // 将当时members元素的子元素member所对应的bean 经过调用其parent members所对应的Members实例
 // 中的办法 addMember,并以其所对应的bean作为参数传入,这样就可以在Members中初始化member的实例了
 digester.addSetNext("members/member", "addMember");
 // 遇到members元素的子元素member中的skill节点时调用其直接parent member实例中的addSkill办法
 // 第三个参数为xml的参数索引,这儿 0 表明去取skill元素body内的值,而且取出的只能是String类型(假如是数字,而addSkill中的参数为int类型,这样会抛No such accessible method exception,
 // 就是说默许只知道String类型的参数,改成String类型参数就能取到,看下面的level就知道)
 digester.addCallMethod("members/member/skill", "addSkill", 0);
 // 当需求参入不同类型的多个参数时,这样用
 digester.addCallMethod("members/member/equipment", "addEquipment", 3, 
 new String[]{"java.lang.String", "java.lang.Integer", "java.lang.String"});
 // 符号equipment 元素 body中的值为参数一
 digester.addCallParam("members/member/equipment", 0);
 // 符号equipment 元素 特点id的值为参数二
 digester.addCallParam("members/member/equipment", 1, "id");
 // 符号equipment 元素 特点version的值为参数三
 digester.addCallParam("members/member/equipment", 2, "version");
 // 抛No such accessible method: setLevel() on object: org.oham.xml.Member,setLevel中传入的是int类型参数,它不认
 //digester.addCallMethod("members/member/level", "setLevel", 0);
 //处理1:调用CallParam符号参数
 digester.addCallMethod("members/member/level", "setLevel", 1, 
 new String[]{"java.lang.Integer"});
 digester.addCallParam("members/member/level", 0);
 //处理2:调用addBeanPropertySetter,去call bean中相应的serter办法
 //digester.addBeanPropertySetter("members/member/level","level");
 return digester.parse(xmlFile);
 }

 

测验块代码:

public static void main(String[] args) {
 try {
 // 读入xml文件
 String fPath = MembersParser.class.getClass().getResource("/org/oham/xml/test-members.xml").getPath();
 File xmlFile = new File(fPath); 
 //办法一
 Members members = new MembersParser().parse(xmlFile);
 //办法二
 //Members members = new MembersParser().parseInXMLRule(xmlFile);
 List Member mList = members.getMembers();
 assert mList.size()  2 : mList.size();
 assert mList.get(0).getSkills().size()  1 : mList.get(0).getSkills().size();
 assert mList.get(0).getEquipments().size()  2 : mList.get(0).getEquipments().size();
 assert mList.get(0).getLevel()  6 : mList.get(0).getLevel();
 assert mList.get(1).getSkills().size()  2 : mList.get(1).getSkills().size();
 assert mList.get(1).getEquipments().size()  0 : mList.get(1).getEquipments().size();
 assert mList.get(1).getLevel()  12 : mList.get(1).getLevel();
 assert "Lulu".equals(mList.get(1).getName()) : mList.get(1).getName();
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 } catch (SAXException e) {
 e.printStackTrace();
 }

 

 

 

 // 运用办法二,将XPATH写到另一个xml封装起来,运用FromXmlRulesModule这个类读入xml并用其生成digester实例
 // 运用一个内部类读入并解析rules xml 
 private class RulesModule extends FromXmlRulesModule{
 @Override
 protected void loadRules() {
 loadXMLRules(MembersParser.class.getClass().getResource("/org/oham/xml/test-members-rules.xml"));                          
			
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表亚美娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章