建test.xsdconfig文件
xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config">
sample.xmlbean
建test.xsd文件
encoding="UTF-8"?>
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
name="Customers">
name="customer"
type="customerType"/>
name="customerType">
type="xs:int"/>
type="xs:string"/>
type="xs:string"/>
type="xs:string"/>
type="xs:string"/>
type="addressType"/>
name="addressType">
type="primaryAddressType"/>
type="billingAddressType"/>
name="primaryAddressType">
type="xs:string"/>
type="xs:string"/>
type="xs:string"/>
name="billingAddressType">
type="xs:string"/>
type="xs:string"/>
type="xs:string"/>
type="xs:string"/>
<?xml version="1.0"encoding="UTF-8"?>
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
name="Customers">
name="customer"
type="customerType"/>
name="customerType">
type="xs:int"/>
type="xs:string"/>
type="xs:string"/>
type="xs:string"/>
type="xs:string"/>
type="addressType"/>
name="addressType">
type="primaryAddressType"/>
type="billingAddressType"/>
name="primaryAddressType">
type="xs:string"/>
type="xs:string"/>
type="xs:string"/>
name="billingAddressType">
type="xs:string"/>
type="xs:string"/>
type="xs:string"/>
type="xs:string"/>
运行代码:
scomp -src test\src -out
build\testXmlBean.jar D:\xmlbeans-2.4.0\test\test.xsd -compiler
D:\myeclipse\common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\bin\javac
D:\xmlbeans-2.4.0\test\test.xsdconfig
生成java包testXmlBean.jar
,在build目录下,并生成源代码在test\src目录下面。
新建一个Java
Project——testXMLBeans,并将D:\xmlbeans-2.4.0\lib下的jar文件以及刚才我们生成的testXmlBean.jar加入到testXMLBeans。
新建一个Java Class:xmlBeans.java 源码如下:
import java.io.File;
import sample.xmlbean.*;
import org.apache.xmlbeans.XmlOptions;
public class xmlBeans {
private String filename = null;
public xmlBeans(String filename) {
super();
this.filename = filename;
}
public void Reader() {//实现读操作
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()