? JavaTM Architecture for XML Binding (JAXB) 提供了api 和工具用于自動映射XML文檔和Java對象。
?
? JAXB框架允許開發者執行以下的操作:
?
? 通過schema 生成相應的java 源文件
? 訪問以及更新相應的java 源文件
? 配置java 源文件,生成相應的schema
? JAXB 給了java 開發者一種有效的和標準的方式用于映射xml和java 代碼。java開發者使用JAXB能提供生產力,由于只需要寫很少的代碼,不需要成為xml方面的專家。JAXB 對于開發者來說更容易擴展他們的應用,使用XML或者web services技術。
?
看一個簡單的例子:?
? 從xsd文件生成相應的java 文件
? xsd 文件配置如下:?
? <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
?
? <xs:import namespace="http://www.w3.org/XML/1998/namespace"
??????? schemaLocation="http://www.w3.org/2001/xml.xsd" />
?
? <xs:complexType name="foo">
??? <xs:sequence>
????? <xs:element name="age" type="xs:int" />
??? </xs:sequence>
? </xs:complexType>
?
? <xs:element name="root" type="foo" />
?
? </xs:schema>
? 定義ant build :? 主要的內容如下:
?<path id="classpath">
??? <pathelement path="src" />
??? <pathelement path="classes" />
??? <pathelement path="schemas" />
??? <!--for use with bundled ant-->
??? <fileset dir="${jwsdp.home}" includes="jaxb/lib/*.jar" />
??? <fileset dir="${jwsdp.home}" includes="sjsxp/lib/*.jar" />
??? <fileset dir="${jwsdp.home}" includes="jwsdp-shared/lib/activation.jar" />
??? <fileset dir="${jwsdp.home}" includes="jwsdp-shared/lib/resolver.jar" />
? </path>
? 定義xjc任務,用于從schema 中生成相應的java 文件
? <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
??? <classpath refid="classpath" />
? </taskdef>
? 執行xjc
? <xjc schema="po.xsd" destdir="gen-src">
????? <produces dir="gen-src" includes="**/*.java" />
? </xjc>
? 執行ant 任務后產生的java 文件如下:
? foo.java
? @XmlAccessorType(AccessType.FIELD)
? @XmlType(name = "foo", propOrder = {
??? "age"
})
public class Foo {
??? @XmlElement(type = Integer.class)
??? protected int age;
??? /**
???? * Gets the value of the age property.
???? *
???? */
??? public int getAge() {
??????? return age;
??? }
??? /**
???? * Sets the value of the age property.
???? *
???? */
??? public void setAge(int value) {
??????? this.age = value;
??? }
}
ObjectFactory.java 主要的產生類,作為工廠類
@XmlRegistry
public class ObjectFactory {
??? private final static QName _Root_QNAME = new QName("", "root");
??? /**
???? * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated
???? *
???? */
??? public ObjectFactory() {
??? }
??? /**
???? * Create an instance of {@link Foo }
???? *
???? */
??? public Foo createFoo() {
??????? return new Foo();
??? }
??? /**
???? * Create an instance of {@link JAXBElement }{@code <}{@link Foo }{@code >}}
???? *
???? */
??? @XmlElementDecl(namespace = "", name = "root")
??? public JAXBElement<Foo> createRoot(Foo value) {
??????? return new JAXBElement<Foo>(_Root_QNAME, Foo.class, null, value);
??? }
}
比較簡單的實現,不過主要的工作還是用來通過java類來生成相應的xml文件:
測試方法:
public static void main(String[] args) throws Exception {
??? ??
??????? JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
??????? ObjectFactory of = new ObjectFactory();
??????? Foo foo = new Foo();
??????? foo.setAge(11);
??????? JAXBElement<Foo> e = of.createRoot(foo);
??? ??? //用于輸出元素
??????? Marshaller marshaller = context.createMarshaller();
??????? marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
??????? FileOutputStream stream = new FileOutputStream(new File(args[0]));
??????? marshaller.marshal(e, stream);
??? }
生成的xml如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
??? <age>11</age>
</root>
posted on 2006-11-08 20:49
布衣郎 閱讀(2229)
評論(0) 編輯 收藏 所屬分類:
webservies