- 浏览: 93152 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
grhglj:
能详细一点吗?都加到哪儿呀。
struts2.0中使用图形验证码 -
shingo7:
jotm哪来的2.3版
Spring引用Tomcat的 JTA事务 -
blogaaa:
这样配置完后,事务不能正常回滚,在同一个action中插入记录 ...
Spring引用Tomcat的 JTA事务
一、关于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技术打下良好地基础.
版权声明:Techtarget获Matrix授权发布,如需转载请联系Matrix
作者:叶枫(http://blog.matrix.org.cn/page/叶枫)
原文地址:http://www.matrix.org.cn/resource/article/44/44027_XMLBean.html
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技术打下良好地基础.
版权声明:Techtarget获Matrix授权发布,如需转载请联系Matrix
作者:叶枫(http://blog.matrix.org.cn/page/叶枫)
原文地址:http://www.matrix.org.cn/resource/article/44/44027_XMLBean.html
发表评论
-
Drools与Spring集成 登录测试
2013-07-12 15:27 0转至: http://justsee.iteye.com/b ... -
Drools入门-----------环境搭建,分析Helloworld
2013-07-12 15:20 0转至: http://justsee.iteye.com/b ... -
H2学习
2013-07-12 15:11 0package test.h2; import ja ... -
BlockingQueue队列学习
2013-10-17 10:16 756package test; import java ... -
一个多线程计算器的实现
2013-07-12 14:56 0package test; import java.util ... -
JRobin Core学习
2012-12-04 14:29 4025原文地址: http://www.micmiu.com/ent ... -
Memcached学习——(四)
2012-11-28 17:11 0原文地址: http://snowolf.iteye.com/ ... -
Memcached学习——(三)
2012-11-28 17:10 0原文地址: http://snowolf ... -
Memcached学习——(二)
2012-11-28 17:08 967原文地址: http://snowolf.iteye.com/ ... -
Memcached学习——(一)
2012-11-28 17:07 1015原文地址: http://snowolf.iteye.com/ ... -
redis学习
2012-11-28 17:03 925原文地址:http://snowolf.iteye.com/b ... -
基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
2012-11-28 16:55 780原文地址:http://www.cnblogs.com/jif ... -
JAI处理TIFF格式图片
2012-09-17 17:48 13286懒得多说,直接代码了。。。。 import java.awt. ... -
全面掌握Java的异常处理机制
2009-03-05 17:17 961你觉得自己是一个Java专 ... -
java.lang包概述
2009-03-05 17:02 131944.1. 接口 java.lang.Appendab ... -
select下拉列表动态显示选择公司及部门信息——级联
2008-11-25 12:29 1276//=================公司类方法======= ... -
人民币大小写转
2008-11-25 12:26 1068String HanDigiStr[] = new Strin ... -
身份证号转换15位与18位
2008-11-25 12:24 1600//开始 15位到18位的身份证号转换 //身份证号码由 ... -
使ApplicationResources.properties支持中文
2008-11-25 12:15 1305使ApplicationResources.propertie ... -
JAVA获得网卡MAC地址
2008-11-25 12:06 1072package test; import java.io.B ...
相关推荐
利用XMLBean轻轻松松读写XML-入门教程 利用XMLBean轻轻松松读写XML-入门教程 利用XMLBean轻轻松松读写XML-入门教程 利用XMLBean轻轻松松读写XML-入门教程
转《利用XMLBean轻轻松松读写XML》 XML在Java应用程序里变得越来越重要, 广泛应用于数据存储和交换. 比如我们常见的配置文件,都是以XML方式存储的. XML还应用于Java Message Service和Web Services等技术作为数据...
XMLBean读写操作示例,作为XMLBean入门是很好的例子 Eclipse
XmlBean 使用说明及jar包 XmlBean 使用说明及jar包
xmlbean介绍及应用
xmlbean API
XMLBean详解, 一、关于XML解析 二、XMLBean简介 三、准备XMLBean和XML文档 四、XMLBean使用步骤 ..... 六、利用XMLBean写XML文件
JDOM 操作xml 解析读取xml文件 修改 删除 增添xml指定节点信息的XmlBean
用xmlbean和xmlstream进行xml和javabean的转换案例
xmlBean eclipse插件
xmlbean生成jar xsd文件生成jar常见问题
NULL 博文链接:https://wulinhaoxia5.iteye.com/blog/1294451
xml的解析和xml的生成;正常情况下,每个XML文件都有一个Schema文件,XML Schema文件是一个XML的约束文件,它定义了XML文件...以及对元素和结构的约束. 通俗地讲,如果说XML文件是数据库里的记录,那么Schema就是表结构定义.
xml 与 bean 互相转换 工具类 1、bean类的属性需要遵守BEAN规范,否则 无法 读存BEAN值。 2、bean类的属性如果 是集合 只支持数组以及List、Map、Set。 3、bean类属性(boolean除外)前面两个字母要么全部小写,要么...
xmlbean的多个例子,官网上下载的,大家分享一下
xml 转Java bean工具
jaxb XMl和BEAN互转实例(内含jar包),将jaxb封装成一个Util工具类,只需要通过对BEAN进行简单的注解就可以直接实现xml和bean之间的互转
bean.xmlbean.xml