首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
XMLBEANS的使用总结
时间:
2015-12-11 12:48:36
阅读:
226
评论:
0
收藏:
0
[点我收藏+]
标签:
在本文中,我们将详细了解最好的数据对象XMLBean。从传统角度来说,在Java应用程序中使用XML,就是在从 XML文档向Java导入数据的技术或从数据模型层向XML导出数据技术之间架起了一座桥梁。对于这种方法,Java通常是作为传统数据存储 (RDBMS)和文档之间的中间表示来使用的。本文将研究这种方法的局限性,并介绍另一种选择,即克服了这些局限性的XMLBean,同时也分析了一些 XMLBean的实际应用。
现有 Java/XML解决方案的主要局限性在于,使用XML导致了在开发中的额外开销。也许XML最强大的功能就是可以使数据以一种结构化的、容易阅读的文档 进行传递。实际上,这种功能会引起Web服务的增殖;为了提供某种服务,供应商必须接受某种可读的文档,而不是定义一种自定义的二进制数据格式。对开发者 来说,这通常意味着要用XML分析器来提取相关的部分,把它们转化成Java的表示,然后通过系统使用这些Java对象。难以置信的是,实际上Java中 许多服务的实现并没有使用XML分析器,而是分析了传入的HTTPInputStream并使用了StringTokenizer或此类方法来提取数 据。
在导出端,当一项新业务需要向其他客户端导出数据时,XML通常是候补方法。在这种情况下,最常见的方法就是自由地使用System.out.println()。如果幸运的话,可以使用像Velocity之类的模板工具。
除 了开发的系统开销外,用这种方法处理XML还容易犯错误。即使确实存在表示XML数据的XML架构,那么开发者也不一定会得到类型安全(type- safe)数据所带来的好处,因为使用导入导出方法时,只有当XML数据进入或离开系统时才会应用有效性检验。因此,当开发者可能会选择使用XML架构 时,XML有效性检验只对该文档有效;对Java对象来说,开发者必须在setter方法中手动创建前置条件(pre condition)和后置条件 (post condition)来检验数据。
处理 XML所带来的开发开销和数据有效性检验的困难一起产生了第三个困难:数据的表示不可能改变。当业务需求变化时,使用导入和导出方法所带来的实现就不够灵 活,以至跟不上这些变化。修改架构会导致对语法分析代码、XML生成代码和有效性检验代码进行重构,而这些代码可能分散在系统的许多Java对象中。
这 里有更好的解决方案。XMLBean通过把Java和XML作为整体来克服了这些困难。通过利用XML Schema的功能来提供结构化和约束性数据类 型,开发者可以像Java对象那样直接访问XML文档。通过使用XMLBean,Java开发者不需要花时间来编写导入/导出和有效性检验代码。相 反,XML文档被视作以类似于Java-Bean的方式访问的最好的数据对象。
除了消除了
"粘合"的 代码和手动的数据有效性检验这些优点外,XMLBean在设计时的另一个优点是,它提供了拥有可执行规范的能力。XMLBean提供了在架构和Java类 型系统间的自动映射。对开发者来说,这意味着设计不需要转化成Java的实现。数据模型或对象的设计完全可以在架构规范中完成;XMLBean自动地把架 构类型和它们之间的关系转换成Java对象及其关系。这样当Java应用程序接收到遵守给定架构的XML文档时,XMLBean创建相应的Java实例, 实例是直接在底层文档上创建的,因而原文档不会有任何损失。当Java实例被修改,XMLBean修改文档以保持二者的同步。因此,Java对象和XML 文档是相同的。
为了强调XMLBean的实用性,有必要看一些XMLBean真正发挥作用的实例:
持久性:几乎所有的应用程序都把用户或应用程序的数据存储在某些文件里,这些内容通常存在于XML之中。XMLBean可以使您不需要手动创建Java对象来表示文件内容就可以处理这些文件。
通 讯:优秀的面向服务的体系结构从系统内部提取出数据,并通过在Web服务之间传递的XML文档来表示业务状态。通过XMLBean和 WebLogic Web服务堆栈,Java开发者可以通过Java接口、由成为XMLBean实例的服务使用的XML文档来与服务通讯。
更 好的数据转换对象(DTO):DTO在J2EE应用程序中被广泛使用,通过Session Façade 提供了对实体bean的粗粒度访问。DTO表示 了实体集合状态的快照。在XMLBean出现之前,Java对象和它们的内容必须通过手动检验有效性来创建。现在有了XMLBean,可以只通过编写 XML架构就可创建具有固有的有效性检验的DTO。
这 些示例表明,XMLBean是真正的下一代Java数据对象。通过高性能、开放源代码的实现,XMLBean克服了将XML和Java一起使用的传统困 难,此外,它还提供了有效性检验和可执行规范的功能。最后,通过保持XML文档和Java对象的同步,XMLBean提供了面向服务开发所需的Java和 XML的结合。
利用XMLBean轻轻松松读写XML
一、关于XML解析
XML在Java应用程序里变得越来越重要, 广泛应用于数据存储和交换. 比如我们常见的配置文件,都是以XML方式存储的. XML还应用于 Java Message Service和Web Services等技术作为数据交换.因此,正确读写XML文档是XML应用的基础.
Java提供了SAX和DOM两种方式用于解析XML,但即便如此,要读写一个稍微复杂的XML,也不是一件容易的事.
二、XMLBean简介
Hibernate已经成为目前流行的面向Java环境的对象/关系数据库映射工具.在Hibernate等对象/关系数据库映射工具出现之前,对数据 库的操作是通过JDBC来实现的,对数据库的任何操作,开发人员都要自己写SQL语句来实现. 对象/关系数据库映射工具出现后,对数据库的操作转成对 JavaBean的操作,极大方便了数据库开发. 所以如果有一个类似的工具能够实现将对XML的读写转成对JavaBean的操作,将会简化XML的读 写,即使对XML不熟悉的开发人员也能方便地读写XML. 这个工具就是XMLBean.
三、准备XMLBean和XML文档
XMLBean是Apache的一个开源项目,可以从http:
//www.apache.org下载,最新的版本是2.0. 解压后目录如下:
xmlbean2.
0.0
+---bin
+---docs
+---lib
+---samples
+---schemas
另外还要准备一个XML文档(customers.xml),
在本文的例子里,我们将对这个文档进行读写操作. 文档源码如下:
<?xml version=
"1.0" encoding=
"UTF-8"?>
<Customers>
<customer>
<id>
1</id>
<gender>female</gender>
<firstname>Jessica</firstname>
<lastname>Lim</lastname>
<phoneNumber>
1234567</phoneNumber>
<address>
<primaryAddress>
<postalCode>
350106</postalCode>
<addressLine1>#
25-
1</addressLine1>
<addressLine2>SHINSAYAMA
2-CHOME</addressLine2>
</primaryAddress>
<billingAddress>
<receiver>Ms Danielle</receiver>
<postalCode>
350107</postalCode>
<addressLine1>#
167</addressLine1>
<addressLine2>NORTH TOWER HARBOUR CITY</addressLine2>
</billingAddress>
</address>
</customer>
<customer>
<id>
2</id>
<gender>male</gender>
<firstname>David</firstname>
<lastname>Bill</lastname>
<phoneNumber>
808182</phoneNumber>
<address>
<primaryAddress>
<postalCode>
319087</postalCode>
<addressLine1>
1033 WS St.</addressLine1>
<addressLine2>Tima Road</addressLine2>
</primaryAddress>
<billingAddress>
<receiver>Mr William</receiver>
<postalCode>
672993</postalCode>
<addressLine1>
1033 WS St.</addressLine1>
<addressLine2>Tima Road</addressLine2>
</billingAddress>
</address>
</customer>
</Customers>
这是一个客户的数据模型,每个客户都有客户编号(ID),姓名,性别(gender),电话号码(phoneNumber)和地址,其中地址有两个: 首要地址(PrimaryAddress)和帐单地址(BillingAddress),每个地址有邮编,地址
1,和地址
2组成.其中帐单地址还有收件人(receiver).此外,还要准备一个配置文件(文件名customer.xsdconfig),这个文件的作用我后面会讲,它的内容如下:
<xb:config xmlns:xb=
"http://xml.apache.org/xmlbeans/2004/02/xbean/config">
<xb:namespace>
<xb:
package>sample.xmlbean</xb:
package>
</xb:namespace>
</xb:config>
四、XMLBean使用步骤
和其他面向Java环境的对象/关系数据库映射工具的使用步骤一样,在正式使用XMLBean前,我们要作两个准备.
1. 生成XML Schema文件
什么是XML Schema文件? 正常情况下,每个XML文件都有一个Schema文件,XML Schema文件是一个XML的约束文件,它定义了 XML文件的结构和元素.以及对元素和结构的约束. 通俗地讲,如果说XML文件是数据库里的记录,那么Schema就是表结构定义.
为什么需要这个文件? XMLBean需要通过这个文件知道一个XML文件的结构以及约束,比如数据类型等. 利用这个Schema文 件,XMLBean将会产生一系列相关的Java Classes来实现对XML的操作. 而作为开发人员,则是利用XMLBean产生的 Java Classes来完成对XML的操作而不需要SAX或DOM.怎样产生这个Schema文件呢? 如果对于熟悉XML的开发人员,可以自己来写 这个Schema文件,对于不熟悉XML的开发人员,可以通过一些工具来完成.比较有名的如XMLSPY和Stylus Studio都可以通过XML文 件来生成Schema文件. 加入我们已经生成这个Schema文件(customer.xsd):
<?xml version=
"1.0" encoding=
"UTF-8"?>
<xs:schema xmlns:xs=
"http://www.w3.org/2001/XMLSchema"
elementFormDefault=
"qualified">
<xs:element name=
"Customers">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs=
"unbounded" name=
"customer"
type=
"customerType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name=
"customerType">
<xs:sequence>
<xs:element name=
"id" type=
"xs:int"/>
<xs:element name=
"gender" type=
"xs:string"/>
<xs:element name=
"firstname" type=
"xs:string"/>
<xs:element name=
"lastname" type=
"xs:string"/>
<xs:element name=
"phoneNumber" type=
"xs:string"/>
<xs:element name=
"address" type=
"addressType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name=
"addressType">
<xs:sequence>
<xs:element name=
"primaryAddress" type=
"primaryAddressType"/>
<xs:element name=
"billingAddress" type=
"billingAddressType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name=
"primaryAddressType">
<xs:sequence>
<xs:element name=
"postalCode" type=
"xs:string"/>
<xs:element name=
"addressLine1" type=
"xs:string"/>
<xs:element name=
"addressLine2" type=
"xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name=
"billingAddressType">
<xs:sequence>
<xs:element name=
"receiver" type=
"xs:string"/>
<xs:element name=
"postalCode" type=
"xs:string"/>
<xs:element name=
"addressLine1" type=
"xs:string"/>
<xs:element name=
"addressLine2" type=
"xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
2. 利用scomp来生成Java Classes
scomp是XMLBean提供的一个编译工具,它在bin的目录下. 通过这个工具,我们可以将以上的Schema文件生成Java Classes.scomp的语法如下:-
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文件的位置, 这个文件主要用来制定生成的Java Class的一些文件名规则和Package的名称,在本文,
package是sample.xmlbean
在本文,我是这样运行的:
scomp -src build\src -out build\customerXmlBean.jar schema\customer.xsd
-compiler C:\jdk142_04\bin\javac customer.xsdconfig
这个命令行的意思是告诉scomp生成customerXmlBean.jar,放在build目录下,同时生成源代码放在build\src 下, Schema文件是customer.xsd,xsdconfig文件是customer.xsdconfig.其实, 生成的Java源代码没有 多大作用,我们要的是jar文件.我们先看一下build\src\sample\xmlbean下生成的Classes.
CustomersDocument.java -- 整个XML文档的Java Class映射
CustomerType.java -- 节点sustomer的映射
AddressType.java -- 节点address的映射
BillingAddressType.java -- 节点billingAddress的映射
PrimaryAddressType.java -- 节点primaryAddress的映射
好了,到此我们所有的准备工作已经完成了. 下面就开始进入重点:利用刚才生成的jar文件读写XML.
五、利用XMLBean读XML文件
新建一个Java Project,将XMLBean2.
0.0\lib\下的Jar文件和刚才我们生成的customerXmlBean.jar加入到Project的ClassPath.
新建一个Java Class: CustomerXMLBean. 源码如下:
package com.sample.reader;
import java.io.File;
import sample.xmlbean.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.xmlbeans.XmlOptions;
public
class CustomerXMLBean {
private String filename =
null;
public CustomerXMLBean(String filename) {
super();
this.filename = filename;
}
public
void customerReader() {
try {
File xmlFile =
new File(filename);
CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
CustomerType[] customers = doc.getCustomers().getCustomerArray();
for (
int i =
0; i < customers.length; i++) {
CustomerType customer = customers[i];
println(
"Customer#" + i);
println(
"Customer ID:" + customer.getId());
println(
"First name:" + customer.getFirstname());
println(
"Last name:" + customer.getLastname());
println(
"Gender:" + customer.getGender());
println(
"PhoneNumber:" + customer.getPhoneNumber());
// Primary address
PrimaryAddressType primaryAddress = customer.getAddress().getPrimaryAddress();
println(
"PrimaryAddress:");
println(
"PostalCode:" + primaryAddress.getPostalCode());
println(
"AddressLine1:" + primaryAddress.getAddressLine1());
println(
"AddressLine2:" + primaryAddress.getAddressLine2());
// Billing address
BillingAddressType billingAddress = customer.getAddress().getBillingAddress();
println(
"BillingAddress:");
println(
"Receiver:" + billingAddress.getReceiver());
println(
"PostalCode:" + billingAddress.getPostalCode());
println(
"AddressLine1:" + billingAddress.getAddressLine1());
println(
"AddressLine2:" + billingAddress.getAddressLine2());
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private
void println(String str) {
System.out.println(str);
}
public
static
void main(String[] args) {
String filename =
"F://JavaTest//Eclipse//XMLBean//xml//customers.xml";
CustomerXMLBean customerXMLBean =
new CustomerXMLBean(filename);
customerXMLBean.customerReader();
}
}
运行它,参看输出结果:
Customer#
0
Customer ID:
1
First name:Jessica
Last name:Lim
Gender:female
PhoneNumber:
1234567
PrimaryAddress:
PostalCode:
350106
AddressLine1:#
25-
1
AddressLine2:SHINSAYAMA
2-CHOME
BillingAddress:
Receiver:Ms Danielle
PostalCode:
350107
AddressLine1:#
167
AddressLine2:NORTH TOWER HARBOUR CITY
Customer#
1
Customer ID:
2
First name:David
Last name:Bill
Gender:male
PhoneNumber:
808182
PrimaryAddress:
PostalCode:
319087
AddressLine1:
1033 WS St.
AddressLine2:Tima Road
BillingAddress:
Receiver:Mr William
PostalCode:
672993
AddressLine1:
1033 WS St.
AddressLine2:Tima Road
怎么样,是不是很轻松? XMLBean的威力.
六、利用XMLBean写XML文件
利用XMLBean创建一个XML文档也是一件轻而易举的事.我们再增加一个Method,
请看一下的Java Class:
public
void createCustomer() {
try {
// Create Document
CustomersDocument doc = CustomersDocument.Factory.newInstance();
// Add new customer
CustomerType customer = doc.addNewCustomers().addNewCustomer();
// set customer info
customer.setId(
3);
customer.setFirstname(
"Jessica");
customer.setLastname(
"Lim");
customer.setGender(
"female");
customer.setPhoneNumber(
"1234567");
// Add new address
AddressType address = customer.addNewAddress();
// Add new PrimaryAddress
PrimaryAddressType primaryAddress = address.addNewPrimaryAddress();
primaryAddress.setPostalCode(
"350106");
primaryAddress.setAddressLine1(
"#25-1");
primaryAddress.setAddressLine2(
"SHINSAYAMA 2-CHOME");
// Add new BillingAddress
BillingAddressType billingAddress = address.addNewBillingAddress();
billingAddress.setReceiver(
"Ms Danielle");
billingAddress.setPostalCode(
"350107");
billingAddress.setAddressLine1(
"#167");
billingAddress.setAddressLine2(
"NORTH TOWER HARBOUR CITY");
File xmlFile =
new File(filename);
doc.save(xmlFile);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
修改main method.
public
static
void main(String[] args) {
String filename =
"F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
CustomerXMLBean customerXMLBean =
new CustomerXMLBean(filename);
customerXMLBean.createCustomer();
}
运行,打开customers_new.xml:
<?xml version=
"1.0" encoding=
"UTF-8"?>
<Customers>
<customer>
<id>
3</id>
<gender>female</gender>
<firstname>Jessica</firstname>
<lastname>Lim</lastname>
<phoneNumber>
1234567</phoneNumber>
<address>
<primaryAddress>
<postalCode>
350106</postalCode>
<addressLine1>#
25-
1</addressLine1>
<addressLine2>SHINSAYAMA
2-CHOME</addressLine2>
</primaryAddress>
<billingAddress>
<receiver>Ms Danielle</receiver>
<postalCode>
350107</postalCode>
<addressLine1>#
167</addressLine1>
<addressLine2>NORTH TOWER HARBOUR CITY</addressLine2>
</billingAddress>
</address>
</customer>
</Customers>
七、利用XMLBean修改XML文件
我们再增加一个Method:
public
void updateCustomer(
int id,String lastname) {
try {
File xmlFile =
new File(filename);
CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
CustomerType[] customers = doc.getCustomers().getCustomerArray();
for (
int i =
0; i < customers.length; i++) {
CustomerType customer = customers[i];
if(customer.getId()==id){
customer.setLastname(lastname);
break;
}
}
doc.save(xmlFile);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
main method:
public
static
void main(String[] args) {
String filename =
"F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
CustomerXMLBean customerXMLBean =
new CustomerXMLBean(filename);
customerXMLBean.updateCustomer(
3,
"last");
}
运行之后,我们将会看到客户编号为
3的客户的lastname已经改为last.
八、利用XMLBean删除一个customer
再增加一个Method:
public
void deleteCustomer(
int id) {
try {
File xmlFile =
new File(filename);
CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
CustomerType[] customers = doc.getCustomers().getCustomerArray();
for (
int i =
0; i < customers.length; i++) {
CustomerType customer = customers[i];
if(customer.getId()==id){
customer.setNil() ;
break;
}
}
doc.save(xmlFile);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
main method:
public
static
void main(String[] args) {
String filename =
"F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
CustomerXMLBean customerXMLBean =
new CustomerXMLBean(filename);
customerXMLBean.deleteCustomer(
3);
}
运行,我们将会看到客户编号为
3的客户的资料已经被删除.
九、查询XML
除了本文在以上讲述的,利用XMLBean能轻轻松松完成XML的读写操作外,结合XPath和XQuery,XMLBean还能完成象SQL查询数据库一样方便地查询XML数据. 关于XML查询以及如何创建XML数据库, 我将在另一篇文章里讨论.
十、结束语
XMLBean能帮助我们轻易读写XML,这将有助于我们降低XML的学习和使用,有了这个基础,开发人员将为学习更多地XML相关技术和Web Services,JMS等其他J2EE技术打下良好地基础.
原文:http://youlong05.iteye.com/blog/87324
XMLBEANS的使用总结
标签:
原文地址:http://www.cnblogs.com/langtianya/p/5038455.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!