博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
XML工具——xmlbeans的使用
阅读量:5232 次
发布时间:2019-06-14

本文共 12620 字,大约阅读时间需要 42 分钟。

一、安装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 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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

1 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

2.2 配置文件

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

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

1 
2 3
4
com.xmlbeans
5
6 7

三、生成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 
2
3
张三
4
123
5
20
6
7
8
中国
9
浙江
10
杭州
11
123456
12
13
14
Chinese
15
ZheJiang
16
HangZhou
17
654321
18
19
20

五、解析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      * 解析xml25      * @param xml文件26      * @return userBean27      */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         //获取根节点user39         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     @Override47     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     @Override27     public String toString() {28         return "AddressesBean [chinese=" + chinese + ", english=" + english29                 + "]";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     @Override49     public String toString() {50         return "UserBean [username=" + username + ", password=" + password51                 + ", age=" + age + ", addresses=" + addresses + "]";52     }53 }
UserBean.java

6.3 项目下载地址

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

转载于:https://www.cnblogs.com/s1165482267/p/8117061.html

你可能感兴趣的文章
Python面向对象之:三大特性:继承,封装,多态以及类的约束
查看>>
微信小程序实现类似JQuery siblings()的方法
查看>>
md5sum命令详解
查看>>
[bzoj1004] [HNOI2008] Cards
查看>>
使用 Swoole 来加速你的 Laravel 应用
查看>>
TextWatcher原因activity内存泄漏问题
查看>>
Merge into的使用具体解释-你Merge了没有
查看>>
Linux安装程序Anaconda分析
查看>>
如何在chrome上打开SSL3.0
查看>>
应该是实例化对象的没有对属性赋值时,自动赋值为null,但不是空指针对象引用...
查看>>
从网易与淘宝的font-size思考前端设计稿与工作流
查看>>
原生HttpClient详细使用示例
查看>>
几道面试题
查看>>
搜索引擎-SHODAN
查看>>
Factory Design Pattern
查看>>
python中贪婪与非贪婪
查看>>
guava API整理
查看>>
无锁编程笔记
查看>>
jquery mobile
查看>>
在Windows环境下使用短信猫收发短信的简单配置:
查看>>