XMLBeans 是 Apache 開源的 XML-Java 綁定框架,將 XML Schema (XSD) 編譯為強類型 Java 類,提供比 DOM/SAX 更友好的面向對象操作體驗。本文記錄從下載、環境配置、Schema 生成、scomp 編譯到獲取 Java 類的完整流程。
https://archive.apache.org/dist/xml/xmlbeans/xmlbeans-current.zip(最新 2.6.0)。xmlbeans-2.6.0/ ├── bin/ # scomp、xsdconfig 等工具 ├── lib/ # 運行時依賴 jar ├── docs/ # 文檔 └── schemas/ # 示例 schema
XMLBEANS_HOME=E:\xmlbeans-2.6.0PATH=%PATH%;%XMLBEANS_HOME%\bin<?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>...</customer>
</Customers>
可用 XMLSpy、Stylus Studio 由 XML 反推 XSD,或手寫。關鍵點:elementFormDefault="qualified"、複雜類型嵌套定義。
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Customers">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="customerType" maxOccurs="unbounded"/>
</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>
創建 customer.xsdconfig,指定生成類的包路徑:
<xb:config xmlns:xb="https://xml.apache.org/xmlbeans/2004/02/xbean/config">
<xb:namespace>
<xb:package>sample.xmlbean</xb:package>
</xb:namespace>
</xb:config>
scomp [options] [schemaFile.xsd] [xsdconfig]
主要參數:
-src [dir] 生成的 Java 源碼目錄
-srconly 僅生成源碼,不編譯、不產生 JAR
-out [jarFile] 生成的 JAR 文件名(默認 xmltypes.jar)
-compiler [path] javac 路徑(含空格需加引號)
E:\xmlbeans-2.6.0> scomp -src build/src -out E:/xmlbeans-2.6.0/customerXmlBean.jar ^
schemas/customer/customer.xsd -compiler "C:/Program Files/Java/jdk1.7.0_04/bin/javac" ^
schemas/customer/customer.xsdconfig
注意:路徑含空格必須用雙引號包裹;Windows 下用 ^ 換行,Linux 用 \。
| 生成類/接口 | 對應 XML 節點 | 說明 |
|---|---|---|
| CustomersDocument | <Customers> 根元素 | 整個 XML 文檔入口,提供 getCustomers() |
| CustomerType | <customer> | 客戶對象,含 id、gender、name、address 等屬性 |
| AddressType | <address> | 地址對象 |
| PrimaryAddressType | <primaryAddress> | 首要地址 |
| BillingAddressType | <billingAddress> | 帳單地址,含 receiver |