??? 讀取xml對于應用軟件來說是一個必不可少的工作,當然現在的jdk也提供了很好的處理xml方式,讀寫xml的庫也挺多,包括有名的dom4j,不管使用任何的代碼庫,對于xml只是一個解析工作而已,不能馬上綁定到java 對象。對于對象,每次都需要set 或者get相應的屬性,當然也可以使用map 來保存xml配置。
??? 于是,一種新的處理方式用于對象和xml之間的映射就變得非常需要,還好sun提供了jaxb,一種很方便的方式來處理java對象和xml內容。下面通過一個實例來體會一下。
??? 看一下如下的xml
<?xml version="1.0"?>
<customer id="No1">
??? <name>Alice Smith</name>
??? <address>
??? ??? <street>123 Maple Street</street>
??? ??? <city>Cambridge</city>
??? ??? <zip>12345</zip>
??? </address>
</customer>
別忘了生成相應的xsd,或者dtd文件,這是主要的配置:
xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
???
??? <xs:complexType name="Customer">
????? <xs:sequence>
??? ??? ?<xs:element name="address" type="Address"/>
???????? <xs:element name="name" type="xs:string"/>
??? ? </xs:sequence>
??? ?? <xs:attribute name="id" type="xs:string"/>??? ???
??? </xs:complexType>?
???
??? <xs:complexType name="Address">
????? <xs:sequence>
??? ??? ?<xs:element name="street" type="xs:string"/>
???????? <xs:element name="city" type="xs:string"/>
???????? <xs:element name="zip" type="ZipCodeType"/>
??? ?? </xs:sequence>
?? </xs:complexType>?
??
??? <xs:simpleType name="ZipCodeType">
????? <xs:restriction base="xs:integer">
???????? <xs:minInclusive value="10000"/>
???????? <xs:maxInclusive value="99999"/>
????? </xs:restriction>
??? </xs:simpleType>
??? <xs:element name="customer" type="Customer"/>
??? <xs:element name="address" type="Address"/>
</xs:schema>
需要映射兩個java對象,CustomerBo和AddressBo
java 對象可以通過xjc來生成。
或者自己定義(但需要增加相應的java注釋,如@XmlAccessorType,@XmlType,這是給引擎使用的)
所以一般通過xjd自動生成
@XmlAccessorType(AccessType.FIELD)
@XmlType(name = "Customer", propOrder = {
??? "address",
??? "customerName"
})
public class CustomerBo {
??? protected Address address;
??? @XmlElement(name = "name")
??? protected String customerName;
??? @XmlAttribute
??? protected String id;
??? public Address getAddress() {
??????? return address;
??? }
??? public String getCustomerName() {
??????? return customerName;
??? }
??? public String getId() {
??????? return id;
??? }
??? public void setAddress(Address value) {
??????? this.address = value;
??? }
??? public void setCustomerName(String value) {
??????? this.customerName = value;
??? }
??? public void setId(String value) {
??????? this.id = value;
??? }
}
public class Address {
??? protected String street;
??? protected String city;
??? @XmlElement(name = "zip")
??? protected BigInteger zipCode;
??? public String getStreet() {
??????? return street;
??? }
??? public void setStreet(String value) {
??????? this.street = value;
??? }
??? public String getCity() {
??????? return city;
??? }
??? public void setCity(String value) {
??????? this.city = value;
??? }
??? public BigInteger getZipCode() {
??????? return zipCode;
??? }
??? public void setZipCode(BigInteger value) {
??????? this.zipCode = value;
??? }
}
定義jxb綁定文件:
<jxb:bindings version="1.0"
?????????????? xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
?????????????? xmlns:xs="http://www.w3.org/2001/XMLSchema">
?? <jxb:bindings schemaLocation="customer.xsd" node="/xs:schema">???
??????????
?? <jxb:globalBindings
???????? fixedAttributeAsConstantProperty="false"
???????? collectionType="java.util.Vector"
???????? typesafeEnumBase="xs:NCName"
???????? choiceContentProperty="false"
???????? typesafeEnumMemberName="generateError"
???????? enableFailFastCheck="false"??
???????? generateIsSetMethod="false"
???????? underscoreBinding="asCharInWord"/>
?? <jxb:schemaBindings>
????? <jxb:package name="mycompany.demo">
??????? <jxb:javadoc><![CDATA[<body>Package level documentation for generated package mycompany.demo.</body>]]>
??? ??? </jxb:javadoc>
????? </jxb:package>
????? <jxb:nameXmlTransform>
??????? <jxb:elementName suffix="Element"/>
????? </jxb:nameXmlTransform>
??? </jxb:schemaBindings>??
??? //需要綁定的元素
?? <jxb:bindings
node="http://xs:complexType[@name='Customer']">
??? ?? //綁定的類
????? <j
xb:class name="CustomerBo">
??????? <jxb:javadoc>A <b>todo..</jxb:javadoc>
????? </jxb:class>
????? <jxb:bindings node=".//xs:element[@name='name']">
?????????? //綁定的屬性
??????????
<jxb:property name="customerName"/>??????? </jxb:bindings>
??? </jxb:bindings>??
?????????
??? <jxb:bindings node="http://xs:complexType[@name='Address']">
????? <jxb:class name="AddressBo">
??????? <jxb:javadoc><![CDATA[First line of documentation for a <b>Address</b>.]]></jxb:javadoc>
????? </jxb:class>
????? <jxb:bindings node=".//xs:element[@name='zip']">
???????? <jxb:property name="zipCode"/>
????? </jxb:bindings>
??? </jxb:bindings>?
????????????
?? </jxb:bindings>
</jxb:bindings>
看著比較復雜,其實挺好理解,當然可以不需要這個綁定文件,也可以綁定相應的java 類,但需要元素名稱和類名稱完全一致,而且屬性也要一致。
看一下jaxb是如何來讀入xml的:
??? ??? //主要的環境類,主要讀取ObjectFactory這個類,這是由xjc生成的。
??????? JAXBContext jc = JAXBContext.newInstance("mycompany.demo");
??????? Unmarshaller u = jc.createUnmarshaller();
??????? JAXBElement customerE = (JAXBElement) u.unmarshal(new FileInputStream(
??????????????????????????????? "customer.xml"));
??????? CustomerBo bo = (CustomerBo) customerE.getValue();
就是這么簡單
寫入也比較簡單:
??????? JAXBContext jc = JAXBContext.newInstance("mycompany.demo");
??? ??? Marshaller marshaller=jc.createMarshaller();
??????? marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
??????? customerE.setValue(bo);
??????? marshaller.marshal( customerE,new FileOutputStream("test.xml"));
在webservices中jaxb的作用是明顯的,當然也有不方便的地方,比如定義binding.jaxb文件時,如果沒有工具支持,手工寫,還是比較困難。
posted on 2006-11-11 20:20
布衣郎 閱讀(4393)
評論(1) 編輯 收藏 所屬分類:
webservies