- 浏览: 298427 次
- 性别:
- 来自: 西安
文章分类
最新评论
-
guyinyihun:
好用,谢谢分享
[转]java 类里判断字符串是iso-8859-1还是gb2312,utf-8,gbk等,判断编码类型 -
java小叶檀:
可以使用hashset retainAll实现
找相同元素 -
爱拼才会赢小超人-1983:
...
UUID -
tuspark:
这篇《serialversionuId作用》解释的更清楚,更有 ...
为何加入:private static final long serialVersionUID -
futily:
public static void middleRevers ...
java数组反转
三、准备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技术打下良好地基础.
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技术打下良好地基础.
相关推荐
poi操作excel所需jar包及poi源码 包含内容 poi-3.7.jar poi-ooxml-3.7.jar poi-ooxml-schemas-3.7.jar poi-scratchpad-3.7.jar Lib-->commons-logging-1.1.jar lib-->junit-3.8.1.jar ...xmlbens-2.3.0.jar poi3.7源码
知识蒸馏_基于Tensorflow实现的无数据知识蒸馏_附项目源码+流程教程_优质项目分享
这个完整工程给出基于人工智能模型U-net神经网络医学图像分割功能实现,适合基于机器学习和深度学习模型分析的学习资料,并有详细程序说明书。
知识领域: 数据库管理 技术关键词: SQLAlchemy、数据库、ORM、SQL 内容关键词: 数据库连接、数据查询、数据操作 用途: 学习使用Python管理和操作数据库 资源描述: SQLAlchemy官方文档提供了详细的SQLAlchemy ORM介绍、文档和示例代码,适合数据库管理和开发人员使用。
Golang(Gin框架)+websocket 实现的多人聊天室+代码+详细文档
武汉大学弘毅学堂数据库系统实现课程实验.zip
随着互联网日益深入我们的生活,网上商品销售的方式也逐渐被人们所接受,因而集成销售管理功能的网上销售系统也显得更加重要而实用。鉴于此,本课题设计了基于网络超市的商品销售管理系统。首先,论文简单介绍了商品销售管理系统的产生、发展和现状;然后对本系统使用的几项关键技术做了介绍;最后用软件工程的分析方法从系统需求分析、系统设计、数据库设计、系统实现等几个方面对整个系统进行了阐述。同时对系统的功能,包括商品目录显示、用户注册、用户登录、购物车、定单查询、系统维护管理等功能模块进行了实现。本系统主要运用了HTML、JSP编程技术与SQL Server数据库等关键技术进行设计、实现。设计中使用页面模块化思想,并部分采用三层架构思想,使用JavaBean 完成对数据库的操作,对销售管理、系统维护部分使用Servlet技术实现。当然系统还存在许多不足之处有待改进,特别是安全方面的问题还没有很好的考虑和实现。
全球市场百宝书-墨西哥.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
胖东来运营管理-餐饮部操作员实操手册.pdf
此文章的 实战练习,植物大战僵尸收阳光,斗地主手牌选择的源码。
CNC运动控制编程.pdf
渔业船舶水上生产安全事故基本情况报表.doc
2024TikTok_Shop年度趋势报告.zip
JavaScript优质学习资源和工具与案列应用场景.txt
《2022_电商出海营销白皮书》.zip
python爬虫案例
该资源为2021年全国大学生电子设计竞赛F题——送药小车,基本功能描述如下: 1、在小车行驶过程中,会存在数字不能完全进入视野中情况等存在有很多帧误识情况,要进行滤波处理; 2、每一帧要做处理的同时每个目标值的矩形框进行处理,每个目标值的矩形框还要进行滤波; 3、在上位机上不断的优化算法,减少掉帧; 4、在Maixpy IDE中python 的numpy、pandas巴拉巴拉包都调不了; 5、图中最多出现四个,但这里设了读六个矩形框,留两个容错,不然有误识别的直接死机; 6、给下位机传送指令: 一开始初始化识别传送对应的数字; 若为12,则后面不在传送指令; 若为34,则后面路口处传"l"or"r"; 若为5678,则后面两个路口传送两次"l"or“r”。
手写字体的异常检测内含数据集-含说明书(可运行).zip
适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip适用于中小型游戏的通用排行榜后台系统.zip