XML工具——xmlbeans的使用

一、安装xmlbeans

1.下载xmlbeans

下载地址:https://gitee.com/shizuru/xmlbeans-2.6.0

2.解压,此处以解压至D盘根目录为例

3.配置环境变量(路径不能有中文或空格)

(1)配置JAVA_HOME:D:\Java\jdk1.7.0_80

(2)配置XMLBEANS_HOME:D:\xmlbeans-2.6.0

(3)在path中添加%JAVA_HOME%\bin和%XMLBEANS_HOME%\bin

4.验检查安装结果

打开cmd输入scomp -version,如下即安装成功

二、编写XML Schema及其配置文件

2.1 XML Schema文件

XML Schema(后缀为.xsd)是基于XML的DTD替代者,可描述XML文档的结构。它定义了XML文件的结构和元素以及对元素和结构的约束。

以下述User.xml为例

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <user>
 3     <username></username>
 4     <password></password>
 5     <age></age>
 6     <addresses>
 7         <Chinese>
 8             <nationality></nationality>
 9             <cityProvince></cityProvince>
10             <district></district>
11             <address></address>
12         </Chinese>
13         <English>
14             <nationality></nationality>
15             <cityProvince></cityProvince>
16             <district></district>
17             <address></address>
18         </English>
19     </addresses>
20 </user>

若要生成该结构的xml文件,则XML Schema文件应编写为

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 3     <!-- 根节点user -->
 4     <xs:element name="user">
 5         <xs:complexType>
 6             <xs:sequence>
 7                 <!-- user子节点username 类型为String -->
 8                 <xs:element name="username" type="xs:string"/>
 9                 <!-- user子节点password 类型为String -->
10                 <xs:element name="password" type="xs:string"/>
11                 <!-- user子节点age 类型为BigDecimal(数字类型 不推荐使用 此处为举例) -->
12                 <xs:element name="age" type="xs:decimal"/>
13                 <!-- user子节点addresses 类型为自定义类型AddressesType -->
14                 <xs:element name="addresses" type="AddressesType"/>
15             </xs:sequence>
16         </xs:complexType>
17     </xs:element>
18     <!-- 自定义类型AddressesType -->
19     <xs:complexType name="AddressesType">
20         <xs:sequence>
21             <!-- addresses子节点Chinese 类型为自定义类型AddressType -->
22             <xs:element name="Chinese" type="AddressType"/>
23             <!-- addresses子节点English 类型为自定义类型AddressType -->
24             <xs:element name="English" type="AddressType"/>
25         </xs:sequence>
26     </xs:complexType>
27     <!-- 自定义类型AddressType -->
28     <xs:complexType name="AddressType">
29         <xs:sequence>
30             <!-- address子节点nationality 类型为String类型 -->
31             <xs:element name="nationality" type="xs:string"/>
32             <!-- address子节点cityProvince 类型为String类型 -->
33             <xs:element name="cityProvince" type="xs:string"/>
34             <!-- address子节点district 类型为String类型 -->
35             <xs:element name="district" type="xs:string"/>
36             <!-- address子节点address 类型为String类型 -->
37             <xs:element name="address" type="xs:string"/>
38         </xs:sequence>
39     </xs:complexType>
40 </xs:schema>

2.2 配置文件

xsd配置文件(后缀为. xsdconfig)主要用来制定生成的Java Class的一些文件名规则和Package的名称

如下所示,生成的包名为com.xmlbeans

1 <xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config">  
2   
3   <xb:namespace>  
4     <xb:package>com.xmlbeans</xb:package>  
5   </xb:namespace>  
6   
7 </xb:config>

三、生成jar包

以上编写的2个文件放置路径为D:/Java目录下

打开cmd,输入 scomp -out D:/Java/User.jar D:/Java/User.xsd -compiler D:/Java/jdk1.7.0_80/bin/javac D:/Java/User.xsdconfig

该命令的语法格式如下

scomp [options] [dirs]* [schemaFile.xsd]* [service.wsdl]* [config.xsdconfig]*

主要参数说明:

-src [dir]            -- 生成的Java Classes存放目录 
-srconly              -- 不编译Java Classes,不产生Jar文件 
-out [jarFileName]    -- 生成的Jar文件,缺省是xmltypes.jar 
-compiler             -- Java编译器的路径,即Javac的位置 
schemaFile.xsd        -- XML Schema文件位置 
config.xsdconfig      -- xsdconfig文件的位置

因此,命令的意思为在D:/Java目录下生成User.jar,使用的XML Schema为D:/Java目录下的User.xsd,其配置文件为D:/Java目录下的User.xsdconfig,使用的编译器为D:/Java/jdk1.7.0_80/bin/javac

成功运行如下图所示

再次查看D:/Java目录,发现多了一个User.jar

四、构造xml文件

将生成的jar包导入项目,同时也需要依赖jar包xbean.jar,下载地址:https://gitee.com/shizuru/xmlbeans-2.6.0

编写构造类

  1 package com.xbean;
  2 
  3 import java.io.File;
  4 import java.io.IOException;
  5 import java.math.BigDecimal;
  6 
  7 import com.pojo.*;
  8 import com.xmlbeans.*;
  9 import com.xmlbeans.UserDocument.User;
 10 
 11 public class BuildXml {
 12     
 13     public static void main(String[] args) {
 14         
 15         UserBean userBean = getUser();
 16         
 17         //生成DOM对象
 18         UserDocument doc = UserDocument.Factory.newInstance();
 19         //生成根节点user
 20         User user = doc.addNewUser();
 21         
 22         //user子节点username
 23         user.setUsername(userBean.getUsername());
 24         //user子节点password
 25         user.setPassword(userBean.getPassword());
 26         //user子节点age
 27         user.setAge(userBean.getAge());
 28         
 29         //生成user子节点addresses
 30         AddressesType addresses = user.addNewAddresses();
 31         AddressesBean addressesBean = userBean.getAddresses();
 32         
 33         //生成addresses子节点chinese
 34         AddressType chinese = addresses.addNewChinese();
 35         AddressBean chineseAddress = addressesBean.getChinese();
 36         //chinese子节点nationality
 37         chinese.setNationality(chineseAddress.getNationality());
 38         //chinese子节点cityProvince
 39         chinese.setCityProvince(chineseAddress.getCityProvince());
 40         //chinese子节点district
 41         chinese.setDistrict(chineseAddress.getDistrict());
 42         //chinese子节点address
 43         chinese.setAddress(chineseAddress.getAddress());
 44         
 45         //生成addresses子节点english
 46         AddressType english = addresses.addNewEnglish();
 47         AddressBean englishAdress = addressesBean.getEnglish();
 48         //english子节点nationality
 49         english.setNationality(englishAdress.getNationality());
 50         //english子节点cityProvince
 51         english.setCityProvince(englishAdress.getCityProvince());
 52         //english子节点district
 53         english.setDistrict(englishAdress.getDistrict());
 54         //english子节点address
 55         english.setAddress(englishAdress.getAddress());
 56         
 57         //构造的xml文件
 58         File xml = new File("user.xml");
 59         try {
 60             //保存
 61             doc.save(xml);
 62         } catch (IOException e) {
 63             System.out.println("user.xml生成失败");
 64             e.printStackTrace();
 65         }
 66         System.out.println("user.xml生成结束");
 67         
 68     }
 69     
 70     /**
 71      * 模拟实际环境 生成获取UserBean实体类对象
 72      * @return user
 73      */
 74     public static UserBean getUser(){
 75         UserBean user = new UserBean();
 76         user.setUsername("张三");
 77         user.setPassword("123");
 78         user.setAge(new BigDecimal("20"));
 79         
 80         AddressBean chinese = new AddressBean();
 81         chinese.setNationality("中国");
 82         chinese.setCityProvince("浙江");
 83         chinese.setDistrict("杭州");
 84         chinese.setAddress("123456");
 85         
 86         AddressBean english = new AddressBean();
 87         english.setNationality("Chinese");
 88         english.setCityProvince("ZheJiang");
 89         english.setDistrict("HangZhou");
 90         english.setAddress("654321");
 91         
 92         AddressesBean addresses = new AddressesBean();
 93         addresses.setChinese(chinese);
 94         addresses.setEnglish(english);
 95         
 96         user.setAddresses(addresses);
 97         
 98         return user;
 99     }
100 
101 }

运行,结束后获得生成的user.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <user>
 3     <username>张三</username>
 4     <password>123</password>
 5     <age>20</age>
 6     <addresses>
 7         <Chinese>
 8             <nationality>中国</nationality>
 9             <cityProvince>浙江</cityProvince>
10             <district>杭州</district>
11             <address>123456</address>
12         </Chinese>
13         <English>
14             <nationality>Chinese</nationality>
15             <cityProvince>ZheJiang</cityProvince>
16             <district>HangZhou</district>
17             <address>654321</address>
18         </English>
19     </addresses>
20 </user>

五、解析xml文件

以四中构造获得的user.xml为例进行解析

编写解析类

 1 package com.xbean;
 2 
 3 import java.io.File;
 4 
 5 import com.pojo.AddressBean;
 6 import com.pojo.AddressesBean;
 7 import com.pojo.UserBean;
 8 import com.xmlbeans.AddressType;
 9 import com.xmlbeans.AddressesType;
10 import com.xmlbeans.UserDocument;
11 import com.xmlbeans.UserDocument.User;
12 
13 public class ParseXml {
14     
15     public static void main(String[] args) {
16         //解析的xml文件
17         File xml = new File("user.xml");
18         UserBean user = parseXml(xml);
19         
20         System.out.println(user);
21     }
22     
23     /**
24      * 解析xml
25      * @param xml文件
26      * @return userBean
27      */
28     public static UserBean parseXml(File xml){
29         UserBean userBean =  new UserBean();
30         UserDocument doc = null;
31         try {
32             //解析xml获取DOM对象
33             doc = UserDocument.Factory.parse(xml);
34         } catch (Exception e) {
35             System.out.println("解析异常");
36             e.printStackTrace();
37         }
38         //获取根节点user
39         User user = doc.getUser();
40         
41         //将xml中的值set进userBean对象中
42         userBean.setUsername(user.getUsername());
43         userBean.setPassword(user.getPassword());
44         userBean.setAge(user.getAge());
45         
46         AddressesType addresses = user.getAddresses();
47         AddressesBean addressesBean = userBean.getAddresses();
48         
49         AddressType chinese = addresses.getChinese();
50         AddressBean chineseBean = addressesBean.getChinese();
51         chineseBean.setNationality(chinese.getNationality());
52         chineseBean.setCityProvince(chinese.getCityProvince());
53         chineseBean.setDistrict(chinese.getDistrict());
54         chineseBean.setAddress(chinese.getAddress());
55         
56         AddressType english = addresses.getEnglish();
57         AddressBean englishBean = addressesBean.getEnglish();
58         englishBean.setNationality(english.getNationality());
59         englishBean.setCityProvince(english.getCityProvince());
60         englishBean.setDistrict(english.getDistrict());
61         englishBean.setAddress(english.getAddress());
62         
63         return userBean;
64     }
65 
66 }

输出结果:UserBean [username=张三, password=123, age=20, addresses=AddressesBean [chinese=AddressBean [nationality=中国, cityProvince=浙江, district=杭州, address=123456], english=AddressBean [nationality=Chinese, cityProvince=ZheJiang, district=HangZhou, address=654321]]]

六、附录

6.1 项目结构

6.2 com.pojo下的类

 1 package com.pojo;
 2 
 3 public class AddressBean {
 4     /**
 5      * 国家
 6      */
 7     private String nationality = "";
 8     /**
 9      * 城市
10      */
11     private String cityProvince = "";
12     /**
13      * 市区
14      */
15     private String district = "";
16     /**
17      * 详细地址
18      */
19     private String address = "";
20     
21     public String getNationality() {
22         return nationality;
23     }
24     public void setNationality(String nationality) {
25         this.nationality = nationality;
26     }
27     public String getCityProvince() {
28         return cityProvince;
29     }
30     public void setCityProvince(String cityProvince) {
31         this.cityProvince = cityProvince;
32     }
33     public String getDistrict() {
34         return district;
35     }
36     public void setDistrict(String district) {
37         this.district = district;
38     }
39     public String getAddress() {
40         return address;
41     }
42     public void setAddress(String address) {
43         this.address = address;
44     }
45     
46     @Override
47     public String toString() {
48         return "AddressBean [nationality=" + nationality + ", cityProvince="
49                 + cityProvince + ", district=" + district + ", address="
50                 + address + "]";
51     }
52 }
AddressBean.java
 1 package com.pojo;
 2 
 3 public class AddressesBean {
 4     /**
 5      * 中文地址
 6      */
 7     private AddressBean chinese = new AddressBean();
 8     /**
 9      * 英文地址
10      */
11     private AddressBean english = new AddressBean();
12     
13     public AddressBean getChinese() {
14         return chinese;
15     }
16     public void setChinese(AddressBean chinese) {
17         this.chinese = chinese;
18     }
19     public AddressBean getEnglish() {
20         return english;
21     }
22     public void setEnglish(AddressBean english) {
23         this.english = english;
24     }
25     
26     @Override
27     public String toString() {
28         return "AddressesBean [chinese=" + chinese + ", english=" + english
29                 + "]";
30     }
31 }
AddressesBean.java
 1 package com.pojo;
 2 
 3 import java.math.BigDecimal;
 4 
 5 public class UserBean {
 6     /**
 7      * 用户名
 8      */
 9     private String username = "";
10     /**
11      * 密码
12      */
13     private String password = "";
14     /**
15      * 年龄
16      */
17     private BigDecimal age = new BigDecimal("0");
18     /**
19      * 地址
20      */
21     private AddressesBean addresses = new AddressesBean();
22     
23     public String getUsername() {
24         return username;
25     }
26     public void setUsername(String username) {
27         this.username = username;
28     }
29     public String getPassword() {
30         return password;
31     }
32     public void setPassword(String password) {
33         this.password = password;
34     }
35     public BigDecimal getAge() {
36         return age;
37     }
38     public void setAge(BigDecimal age) {
39         this.age = age;
40     }
41     public AddressesBean getAddresses() {
42         return addresses;
43     }
44     public void setAddresses(AddressesBean addresses) {
45         this.addresses = addresses;
46     }
47     
48     @Override
49     public String toString() {
50         return "UserBean [username=" + username + ", password=" + password
51                 + ", age=" + age + ", addresses=" + addresses + "]";
52     }
53 }
UserBean.java

6.3 项目下载地址

https://gitee.com/shizuru/xmlbeans-2.6.0

posted @ 2017-12-26 11:16  x≒y  阅读(9828)  评论(1编辑  收藏  举报