利用commons-digester解析xml

利用commons-digester解析xml

jakarta的commons中的digester是非常优秀的xml解析工具,这个工具提供了从
xml->javabean的映射。相较于传统的w3c、sax方式解析xml文档,digester的层次更高,适合更懒得家伙。
下面这个例子简单,就是一个简单的存储数据,xml文件由schema文件约束,映射到对应的javabean当中,当然功能还不是很完善,作用也不够通用。
schema文件如下:

Xml代码 复制代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < schema   xmlns = "http://www.w3.org/2001/XMLSchema"   
  3.     targetNamespace = "http://www.example.org/final"   
  4.     xmlns:tns = "http://www.example.org/final"   
  5.     elementFormDefault = "qualified" >   
  6.   
  7.     < element   name = "doc" >   
  8.         < complexType >   
  9.             < sequence >   
  10.                 < element   name = "prvs"   type = "tns:prvs-type"   />   
  11.                 < element   name = "usrs"   type = "tns:usrs-type"   />   
  12.             </ sequence >   
  13.         </ complexType >   
  14.     </ element >   
  15.   
  16.     < complexType   name = "prvs-type" >   
  17.         < sequence >   
  18.             < element   name = "prv"   minOccurs = "0"   maxOccurs = "unbounded"   
  19.                 type = "tns:prv-type"   />   
  20.         </ sequence >   
  21.     </ complexType >   
  22.   
  23.     < complexType   name = "usrs-type" >   
  24.         < sequence >   
  25.             < element   name = "usr"   minOccurs = "0"   maxOccurs = "unbounded"   
  26.                 type = "tns:usr-type"   />   
  27.         </ sequence >   
  28.     </ complexType >   
  29.   
  30.     < complexType   name = "prv-type" >   
  31.         < sequence >   
  32.             < element   name = "name"   type = "string"   />   
  33.             < element   name = "descr"   type = "string"   />   
  34.         </ sequence >   
  35.         < attribute   name = "prv_id"   type = "integer"   use = "required"   />   
  36.     </ complexType >   
  37.   
  38.     < complexType   name = "usr-type" >   
  39.         < sequence >   
  40.             < element   name = "name"   type = "string"   />   
  41.             < element   name = "pwd"   type = "string"   />   
  42.             < element   name = "prvs" >   
  43.                 < complexType >   
  44.                     < sequence >   
  45.                         < element   name = "prv_id"   type = "integer"   
  46.                             minOccurs = "0"   maxOccurs = "unbounded"   />   
  47.                     </ sequence >   
  48.                 </ complexType >   
  49.             </ element >   
  50.         </ sequence >   
  51.         < attribute   name = "usr_id"   type = "integer"   use = "required"   />   
  52.     </ complexType >   
  53. </ schema >   
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
	targetNamespace="http://www.example.org/final"
	xmlns:tns="http://www.example.org/final"
	elementFormDefault="qualified">

	<element name="doc">
		<complexType>
			<sequence>
				<element name="prvs" type="tns:prvs-type" />
				<element name="usrs" type="tns:usrs-type" />
			</sequence>
		</complexType>
	</element>

	<complexType name="prvs-type">
		<sequence>
			<element name="prv" minOccurs="0" maxOccurs="unbounded"
				type="tns:prv-type" />
		</sequence>
	</complexType>

	<complexType name="usrs-type">
		<sequence>
			<element name="usr" minOccurs="0" maxOccurs="unbounded"
				type="tns:usr-type" />
		</sequence>
	</complexType>

	<complexType name="prv-type">
		<sequence>
			<element name="name" type="string" />
			<element name="descr" type="string" />
		</sequence>
		<attribute name="prv_id" type="integer" use="required" />
	</complexType>

	<complexType name="usr-type">
		<sequence>
			<element name="name" type="string" />
			<element name="pwd" type="string" />
			<element name="prvs">
				<complexType>
					<sequence>
						<element name="prv_id" type="integer"
							minOccurs="0" maxOccurs="unbounded" />
					</sequence>
				</complexType>
			</element>
		</sequence>
		<attribute name="usr_id" type="integer" use="required" />
	</complexType>
</schema>

 对应2个具体的javabean:
一个是user类,代码如下:

Java代码 复制代码
  1. package  com.cxz.xmldb;  
  2.   
  3. import  java.util.HashSet;  
  4. import  java.util.Set;  
  5. import  java.util.Collections;  
  6.   
  7. public   class  User {  
  8.     private   int  usr_id;  
  9.   
  10.     private  String name;  
  11.   
  12.     private  String pwd;  
  13.   
  14.     private  Set<Integer> prvs_id = Collections  
  15.             .synchronizedSet(new  HashSet<Integer>());  
  16.   
  17.     private  Set<Privilege> prvs = Collections  
  18.             .synchronizedSet(new  HashSet<Privilege>());  
  19.   
  20.     public  String getPwd() {  
  21.         return  pwd;  
  22.     }  
  23.   
  24.     public   void  setPwd(String pwd) {  
  25.         this .pwd = pwd;  
  26.     }  
  27.   
  28.     public  String getName() {  
  29.         return  name;  
  30.     }  
  31.   
  32.     public   void  setName(String name) {  
  33.         this .name = name;  
  34.     }  
  35.   
  36.     public   int  getUsr_id() {  
  37.         return  usr_id;  
  38.     }  
  39.   
  40.     public   void  setUsr_id( int  usr_id) {  
  41.         this .usr_id = usr_id;  
  42.     }  
  43.   
  44.     public   void  addPrv(Privilege priv)  throws  DuplicateException {  
  45.         if  (!prvs.add(priv))  
  46.             throw   new  DuplicateException(  
  47.                     "The privilege had been duplicated created." );  
  48.     }  
  49.   
  50.     public   void  rmvPrv(Privilege priv) {  
  51.         prvs.remove(priv);  
  52.     }  
  53.   
  54.     public   void  addPrvId(Integer priv)  throws  DuplicateException {  
  55.         if  (!prvs_id.add(priv))  
  56.             throw   new  DuplicateException(  
  57.                     "The privilegeId had been duplicated created." );  
  58.     }  
  59.   
  60.     public   void  rmvPrvId(Integer priv) {  
  61.         prvs_id.remove(priv);  
  62.     }  
  63.   
  64.     public  Set<Privilege> getPrvs() {  
  65.         return  prvs;  
  66.     }  
  67.   
  68.     public   void  setPrvs(Set<Privilege> prvs) {  
  69.         this .prvs = prvs;  
  70.     }  
  71.   
  72.     public  Set<Integer> getPrvs_id() {  
  73.         return  prvs_id;  
  74.     }  
  75.   
  76.     public   void  setPrvs_id(Set<Integer> prvs_id) {  
  77.         this .prvs_id = prvs_id;  
  78.     }  
  79.   
  80.     @Override   
  81.     public   int  hashCode() {  
  82.         final   int  PRIME =  31 ;  
  83.         int  result =  1 ;  
  84.         result = PRIME * result + usr_id;  
  85.         return  result;  
  86.     }  
  87.   
  88.     @Override   
  89.     public   boolean  equals(Object obj) {  
  90.         if  ( this  == obj)  
  91.             return   true ;  
  92.         if  (obj ==  null )  
  93.             return   false ;  
  94.         if  (getClass() != obj.getClass())  
  95.             return   false ;  
  96.         final  User other = (User) obj;  
  97.         if  (usr_id != other.usr_id)  
  98.             return   false ;  
  99.         return   true ;  
  100.     }  
  101.   
  102. }  
package com.cxz.xmldb;

import java.util.HashSet;
import java.util.Set;
import java.util.Collections;

public class User {
	private int usr_id;

	private String name;

	private String pwd;

	private Set<Integer> prvs_id = Collections
			.synchronizedSet(new HashSet<Integer>());

	private Set<Privilege> prvs = Collections
			.synchronizedSet(new HashSet<Privilege>());

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getUsr_id() {
		return usr_id;
	}

	public void setUsr_id(int usr_id) {
		this.usr_id = usr_id;
	}

	public void addPrv(Privilege priv) throws DuplicateException {
		if (!prvs.add(priv))
			throw new DuplicateException(
					"The privilege had been duplicated created.");
	}

	public void rmvPrv(Privilege priv) {
		prvs.remove(priv);
	}

	public void addPrvId(Integer priv) throws DuplicateException {
		if (!prvs_id.add(priv))
			throw new DuplicateException(
					"The privilegeId had been duplicated created.");
	}

	public void rmvPrvId(Integer priv) {
		prvs_id.remove(priv);
	}

	public Set<Privilege> getPrvs() {
		return prvs;
	}

	public void setPrvs(Set<Privilege> prvs) {
		this.prvs = prvs;
	}

	public Set<Integer> getPrvs_id() {
		return prvs_id;
	}

	public void setPrvs_id(Set<Integer> prvs_id) {
		this.prvs_id = prvs_id;
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + usr_id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final User other = (User) obj;
		if (usr_id != other.usr_id)
			return false;
		return true;
	}

}

 

 
另一个类是privilege类:

Java代码 复制代码
  1. package  com.cxz.xmldb;  
  2.   
  3. public   class  Privilege {  
  4.     private   int  prv_id;  
  5.   
  6.     private  String name;  
  7.   
  8.     private  String descr;  
  9.   
  10.     public  String getDescr() {  
  11.         return  descr;  
  12.     }  
  13.   
  14.     public   void  setDescr(String descr) {  
  15.         this .descr = descr;  
  16.     }  
  17.   
  18.     public  String getName() {  
  19.         return  name;  
  20.     }  
  21.   
  22.     public   void  setName(String name) {  
  23.         this .name = name;  
  24.     }  
  25.   
  26.     public   int  getPrv_id() {  
  27.         return  prv_id;  
  28.     }  
  29.   
  30.     public   void  setPrv_id( int  prv_id) {  
  31.         this .prv_id = prv_id;  
  32.     }  
  33.   
  34.     @Override   
  35.     public   int  hashCode() {  
  36.         final   int  PRIME =  31 ;  
  37.         int  result =  1 ;  
  38.         result = PRIME * result + prv_id;  
  39.         return  result;  
  40.     }  
  41.   
  42.     @Override   
  43.     public   boolean  equals(Object obj) {  
  44.         if  ( this  == obj)  
  45.             return   true ;  
  46.         if  (obj ==  null )  
  47.             return   false ;  
  48.         if  (getClass() != obj.getClass())  
  49.             return   false ;  
  50.         final  Privilege other = (Privilege) obj;  
  51.         if  (prv_id != other.prv_id)  
  52.             return   false ;  
  53.         return   true ;  
  54.     }  
  55. }  
package com.cxz.xmldb;

public class Privilege {
	private int prv_id;

	private String name;

	private String descr;

	public String getDescr() {
		return descr;
	}

	public void setDescr(String descr) {
		this.descr = descr;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPrv_id() {
		return prv_id;
	}

	public void setPrv_id(int prv_id) {
		this.prv_id = prv_id;
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + prv_id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Privilege other = (Privilege) obj;
		if (prv_id != other.prv_id)
			return false;
		return true;
	}
}

 下面这个是xml数据文件,包括了具体的关系:

Xml代码 复制代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < doc   xmlns = "http://www.example.org/final"   
  3.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  4.     xsi:schemaLocation = "http://www.example.org/final doc.xsd " >   
  5.     < prvs >   
  6.         < prv   prv_id = "0" >   
  7.             < name > read </ name >   
  8.             < descr > read the file </ descr >   
  9.         </ prv >   
  10.         < prv   prv_id = "1" >   
  11.             < name > write </ name >   
  12.             < descr > write the file </ descr >   
  13.         </ prv >   
  14.         < prv   prv_id = "2" >   
  15.             < name > execute </ name >   
  16.             < descr > execute the file </ descr >   
  17.         </ prv >   
  18.     </ prvs >   
  19.     < usrs >   
  20.         < usr   usr_id = "2003710201" >   
  21.             < name > cxz </ name >   
  22.             < pwd > 19841230 </ pwd >   
  23.             < prvs >   
  24.                 < prv_id > 0 </ prv_id >   
  25.                 < prv_id > 1 </ prv_id >   
  26.                 < prv_id > 2 </ prv_id >   
  27.             </ prvs >   
  28.         </ usr >   
  29.         < usr   usr_id = "2003710202" >   
  30.             < name > wm </ name >   
  31.             < pwd > 19841230 </ pwd >   
  32.             < prvs >   
  33.                 < prv_id > 0 </ prv_id >   
  34.                 < prv_id > 1 </ prv_id >   
  35.             </ prvs >   
  36.         </ usr >   
  37.         < usr   usr_id = "2003710203" >   
  38.             < name > wjt </ name >   
  39.             < pwd > 19841230 </ pwd >   
  40.             < prvs >   
  41.                 < prv_id > 1 </ prv_id >   
  42.                 < prv_id > 2 </ prv_id >   
  43.             </ prvs >   
  44.         </ usr >   
  45.     </ usrs >   
  46. <!--  
  47.     < ulist >   
  48.         yxiong  =  role ,role2...  
  49.         ben  =  role2   
  50.     </ ulist >   
  51.       
  52.     < pri >   
  53.         role  =  a .jsp,b.action,....  
  54.     </ pri >   
  55.  -->        
  56. </ doc >   
<?xml version="1.0" encoding="UTF-8"?>
<doc xmlns="http://www.example.org/final"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.example.org/final doc.xsd ">
	<prvs>
		<prv prv_id="0">
			<name>read</name>
			<descr>read the file</descr>
		</prv>
		<prv prv_id="1">
			<name>write</name>
			<descr>write the file</descr>
		</prv>
		<prv prv_id="2">
			<name>execute</name>
			<descr>execute the file</descr>
		</prv>
	</prvs>
	<usrs>
		<usr usr_id="2003710201">
			<name>cxz</name>
			<pwd>19841230</pwd>
			<prvs>
				<prv_id>0</prv_id>
				<prv_id>1</prv_id>
				<prv_id>2</prv_id>
			</prvs>
		</usr>
		<usr usr_id="2003710202">
			<name>wm</name>
			<pwd>19841230</pwd>
			<prvs>
				<prv_id>0</prv_id>
				<prv_id>1</prv_id>
			</prvs>
		</usr>
		<usr usr_id="2003710203">
			<name>wjt</name>
			<pwd>19841230</pwd>
			<prvs>
				<prv_id>1</prv_id>
				<prv_id>2</prv_id>
			</prvs>
		</usr>
	</usrs>
<!--
	<ulist>
		yxiong = role,role2...
		ben = role2
	</ulist>
	
	<pri>
		role = a.jsp,b.action,....
	</pri>
 -->		
</doc>

 这个类代表了整个文档

Java代码 复制代码
  1. package  com.cxz.xmldb;  
  2.   
  3. import  java.util.Collections;  
  4. import  java.util.HashSet;  
  5. import  java.util.Iterator;  
  6. import  java.util.Set;  
  7.   
  8. public   class  Document {  
  9.     private  Set<Privilege> prvs = Collections  
  10.             .synchronizedSet(new  HashSet<Privilege>());  
  11.   
  12.     private  Set<User> usrs = Collections.synchronizedSet( new  HashSet<User>());  
  13.   
  14.     public  Document() {  
  15.     }  
  16.   
  17.     public   void  addPrivilege(Privilege p)  throws  DuplicateException {  
  18.         if  (!prvs.add(p))  
  19.             throw   new  DuplicateException( "Duplicated Priv" );  
  20.     }  
  21.   
  22.     public   void  removePrivilege(Privilege p) {  
  23.         prvs.remove(p);  
  24.     }  
  25.   
  26.     public   void  addUser(User u)  throws  DuplicateException {  
  27.         if  (!usrs.add(u))  
  28.             throw   new  DuplicateException( "Duplicated Usr" );  
  29.     }  
  30.   
  31.     public   void  removeUser(User u) {  
  32.         usrs.remove(u);  
  33.     }  
  34.   
  35.     public   void  fetch()  throws  DuplicateException, Exception {  
  36.         for  (User usr : usrs) {  
  37.             for (Integ
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值