閑人野居
          好好學習,天天向上
          posts - 57,  comments - 137,  trackbacks - 0
          ? 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 布衣郎 閱讀(2231) 評論(0)  編輯  收藏 所屬分類: webservies

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2006年11月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(12)

          隨筆分類(59)

          隨筆檔案(57)

          blog

          java

          uml

          搜索

          •  

          積分與排名

          • 積分 - 357332
          • 排名 - 155

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 丰镇市| 阳江市| 招远市| 新郑市| 澳门| 海盐县| 什邡市| 揭西县| 得荣县| 舒城县| 常州市| 方正县| 东乌珠穆沁旗| 钦州市| 襄樊市| 海兴县| 定结县| 东至县| 崇左市| 延吉市| 永川市| 米泉市| 新民市| 西华县| 云阳县| 南投市| 阿坝| 大安市| 土默特右旗| 曲阳县| 桃园县| 洛扎县| 柞水县| 兴义市| 昭通市| 上犹县| 奈曼旗| 天柱县| 肇源县| 五家渠市| 潮州市|