XMLBean能够实现将对XML的读写转成对JavaBean的操作,将会简化XML的读写,即使对XML不熟悉的开发人员也能方便地读写XML.
在工程目录创建lib文件夹,将xbean.jar拷到该文件夹。
XMLBean是Apache的一个开源项目,可以从http://www.apache.org下载,最新的版本是2.0. 解压后目录如下:
xmlbean2.0.0
+---bin
+---docs
+---lib
+---samples
+---schemas
好了,下面贴上代码。
在工程目录创建schema文件夹,然后在该文件夹创建customers.xml和customer.xsd文件。
customers.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<bean:Customers xsi:schemaLhttp://smaple/xmlbean/">http://smaple/xmlbean/ customer.xsd"
xmlns:http://smaple/xmlbean/">http://smaple/xmlbean/" xmlns:http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance">
<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>
</bean:Customers>
customer.xsd文件
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:bean="http://smaple/xmlbean/" targetNamespace="http://smaple/xmlbean/">
<xs:element name="Customers">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="customer"
type="bean: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="bean:addressType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="addressType">
<xs:sequence>
<xs:element name="primaryAddress" type="bean:primaryAddressType" />
<xs:element name="billingAddress" type="bean: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>
在工程目录创建xbuild.xml文件。
xbuild.xml文件
<project name="XMLBean" default="buildAll" basedir=".">
<taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean"
classpathref="lib.path" />
<property name="build.dir" value="xbuild" />
<property name="schema.dir" value="schema" />
<property name="src.dir" value="${build.dir}/src" />
<path id="lib.path">
<pathelement location="." />
<fileset dir="lib">
<include name="**/*.jar" />
<include name="**/*.zip" />
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="init" depends="clean">
<echo message="----------- Generate XML Bean Message Objects ------------" />
<filter token="verbose" value="true" />
</target>
<target name="prepare" depends="init">
<mkdir dir="${build.dir}" />
</target>
<target name="buildAll" depends="build">
</target>
<!-- all builders -->
<target name="build" depends="prepare">
<xmlbean schema="${schema.dir}/customer.xsd" destfile="${build.dir}/customerXmlBean.jar"
classpathref="lib.path" srcgendir="${src.dir}" />
<copy file="${build.dir}/customerXmlBean.jar" todir="lib" />
</target>
</project>
CustomerXMLBean.java文件
import java.io.File;
import smaple.xmlbean.BillingAddressType;
import smaple.xmlbean.CustomerType;
import smaple.xmlbean.CustomersDocument;
import smaple.xmlbean.PrimaryAddressType;
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;
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 dir = System.getProperty("user.dir");
String filename = dir + File.separator + "schema" + File.separator
+ "customers.xml";
CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
customerXMLBean.customerReader();
}
}