betwixt 解析XML

转自:http://salever.iteye.com/blog/710936

使用apche 的 betwixt,相关库为

Ø      commons-beanutils.jar

Ø      commons-betwixt-1.0-beta-1.jar

Ø      commons-collections-3.2.1.jar

Ø      commons-configuration-1.6.jar

Ø      commons-digester.jar

Ø      commons-lang-2.4.jar

Ø      commons-logging.jar

比如解析如下XML:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <class id="1" name="class one">  
  3.    <students>  
  4.          <student name="Jime">  
  5.                  <score maths="80" />  
  6.             </student>  
  7.          <student name="Abby">  
  8.                    <score maths="100" />  
  9.       </student>  
  10.      </students>  
  11.  </class>  

 结构很简单,这里使用betwixt 方式进行解析。

首先封装XML 的元素信息,主要为3个类:

Class.java

Java代码   收藏代码
  1. public class Class {  
  2.   
  3.        private int id;  
  4.   
  5.        private String name;  
  6.   
  7.        private List<Student> students = new ArrayList<Student>();  
  8.   
  9.        /** 
  10.         * Add a student. 
  11.         * @param student 
  12.         */  
  13.        public void addStudent(Student student){  
  14.           this.students.add(student);  
  15.        }      
  16.   
  17.        /** 
  18.         * Remove a student. 
  19.         * @param student 
  20.         */  
  21.        public void removeStudent(Student student){  
  22.               this.students.remove(student);  
  23.        }  
  24.   
  25.        /** 
  26.         * @return the id 
  27.         */  
  28.   
  29.        public int getId() {  
  30.               return id;  
  31.        }  
  32.   
  33.        /** 
  34.         * @param id the id to set 
  35.         */  
  36.        public void setId(int id) {  
  37.               this.id = id;  
  38.        }  
  39.   
  40.        /** 
  41.         * @return the name 
  42.         */  
  43.        public String getName() {  
  44.               return name;  
  45.        }  
  46.   
  47.        /** 
  48.         * @param name the name to set 
  49.         */  
  50.        public void setName(String name) {  
  51.               this.name = name;  
  52.        }  
  53.   
  54.        /** 
  55.         * @return the students 
  56.         */  
  57.        public List<Student> getStudents() {  
  58.               return students;  
  59.        }  
  60.   
  61.        /** 
  62.         * @param students the students to set 
  63.         */  
  64.        public void setStudents(List<Student> students) {  
  65.               this.students = students;  
  66.        }  
  67.   
  68. }  

 Student.java

Java代码   收藏代码
  1. public class Score {  
  2.   
  3.        private int mathScore;  
  4.   
  5.        /** 
  6.         * @return the mathScore 
  7.         */  
  8.        public int getMathScore() {  
  9.               return mathScore;  
  10.        }  
  11.   
  12.        /** 
  13.         * @param mathScore the mathScore to set 
  14.         */  
  15.        public void setMathScore(int mathScore) {  
  16.               this.mathScore = mathScore;  
  17.        }  
  18.   
  19. }  

 

都很简单,就不用多描述了。下面是每个类对应的XML 描述文件:

Class.betwixt:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <info primitiveTypes="attribute">  
  3.        <element name="class">  
  4.            <attribute name="id" property="id"/>  
  5.              <attribute name="name" property="name"/>  
  6.               <element name="students">  
  7.                  <element name="student" property="student"/>  
  8.               </element>             
  9.               <addDefaults/>  
  10.        </element>  
  11. </info>  

 Student.betwixt:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <info primitiveTypes="attribute">  
  3.        <element name="student">  
  4.               <attribute name="name" property="name"/>        
  5.               <element name="score" property="score"/>         
  6.               <addDefaults/>  
  7.        </element>  
  8. </info>  

 Score.betwixt:

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <info primitiveTypes="attribute">  
  3.        <element name="score">  
  4.               <attribute name="maths" property="mathScore"/>  
  5.               <addDefaults/>  
  6.        </element>  
  7. </info>  

 这些XML 文件名与类名相同,用来在XML 文件与实体类之间进行映射。具体的解析方法就很简单了。注意这些文件需要跟.java文件放到同一目录下。

 

Java代码   收藏代码
  1. public static Object fromXml(InputStream in, Class clazz)  
  2.                      throws XmlException {  
  3.   
  4.               BeanReader beanReader = new BeanReader();  
  5.              Object object = null;  
  6.               try {  
  7.                      beanReader.registerBeanClass(clazz);  
  8.                     beanReader.getXMLIntrospector().setWrapCollectionsInElement(true);  
  9.                      object = beanReader.parse(in);  
  10.               } catch (Exception e) {  
  11.                      throw new XmlException(e);  
  12.               }  
  13.               return object;  
  14.        }  

 使用fromXml 解析文件:

Java代码   收藏代码
  1. public static Class parseFromXml(String path) throws Exception {  
  2.   
  3.               FileInputStream in;  
  4.               Class obj = null;  
  5.               try {  
  6.                      in = new FileInputStream(path);  
  7.                      obj = (Class) XmlUtil.fromXml(in, Class.class);  
  8.               } catch (FileNotFoundException e) {  
  9.                      throw new Exception(e);  
  10.               } catch (Exception e) {  
  11.                      throw new Exception(e);  
  12.               }  
  13.   
  14.               return obj;  
  15.        }  

 输出为XML:

Java代码   收藏代码
  1. public static void toXml(Object object, OutputStream out)  
  2.                      throws Exception {  
  3.               try {  
  4.                      BeanWriter beanWriter = new BeanWriter(out, "UTF-8");                 
  5. beanWriter.enablePrettyPrint();  
  6. beanWriter.setIndent("    ");                                                
  7. beanWriter.setEndOfLine(System.getProperty("line.separator"));                     
  8. beanWriter.writeXmlDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");     
  9.                   beanWriter.getXMLIntrospector().setWrapCollectionsInElement(true);  
  10.                      beanWriter.setWriteEmptyElements(false);  
  11.                      beanWriter.write(object);  
  12.                      beanWriter.close();  
  13.               } catch (Exception e) {  
  14.                      throw new XmlException(e);  
  15.               }  
  16.    }  

 
将Class 类的对象输出为XML:

Java代码   收藏代码
  1. public static void writeToXml(Object object, String path)  
  2.                      throws Exception {  
  3.   
  4.               try {  
  5.                      FileOutputStream out = new FileOutputStream(path);  
  6.                      XmlUtil.toXml(object, out);  
  7.                      out.close();  
  8.               } catch (FileNotFoundException e) {  
  9.                      throw new Exception(e);  
  10.               } catch (IOException e) {  
  11.                      e.printStackTrace();  
  12.               }  
  13.        }  

 完成XML 解析的过程很简单,就只有几句话而以,这对于大型复杂的XML 解析很有帮助,不需要亲自处理各种属性、元素等,因为各种.jar 文件已经封装好了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值