jaxb 生成java类_重用生成的JAXB类
jaxb 生成java類
在本文中,我將演示如何利用XJC擴展來重用以前從XML模式生成的類。 當其他XML架構導入XML架構并且您不想每次都生成相同的類時,這很有用。導入的架構(Product.xsd)
以下XML模式代表有關產品的基本信息。 產品是此示例域中的通用概念,因此我決定定義一種可以被其他模式利用的表示形式,而不是讓每個模式都定義自己的產品信息表示形式。
<?xml version="1.0" encoding="UTF-8"?> <schemaxmlns="http://www.w3.org/2001/XMLSchema "targetNamespace=" http://www.example.org/Product "xmlns:tns=" http://www.example.org/Product "elementFormDefault="qualified"><element name="product"><complexType><sequence><element name="id" type="string"/><element name="name" type="string"/></sequence></complexType></element> </schema>由于多個XML模式導入Product.xsd,我們可以利用情節文件,以便與Product.xsd對應的類僅生成一次。 以下XJC調用演示了如何生成稱為product的情節文件。 情節以及生成的類:
xjc -d out -episode product.episode Product.xsd導入架構(ProductPurchaseRequest.xsd)
以下是導入Product.xsd的XML模式的示例:
<?xml version="1.0" encoding="UTF-8"?> <schemaxmlns="http://www.w3.org/2001/XMLSchema "targetNamespace=" http://www.example.org/ProductPurchaseRequest "xmlns:tns=" http://www.example.org/ProductPurchaseRequest "xmlns:prod=" http://www.example.org/Product "elementFormDefault="qualified"><import namespace=" http://www.example.org/Product " schemaLocation="Product.xsd"/><element name="purchase-request"><complexType><sequence><element ref="prod:product" maxOccurs="unbounded"/></sequence></complexType></element> </schema>從XML模式生成類時,將引用從Product.xsd生成Java類時創建的情節文件。 如果我們未指定情節文件,則將為ProductPurchaseRequest.xsd和Product.xsd生成類:
另一個導入模式(ProductQuoteRequest.xsd)
以下是導入Product.xsd的XML模式的另一個示例:
<?xml version="1.0" encoding="UTF-8"?> <schemaxmlns="http://www.w3.org/2001/XMLSchema "targetNamespace=" http://www.example.org/ProductQuoteRequest "xmlns:tns=" http://www.example.org/ProductQuoteRequest "xmlns:prod=" http://www.example.org/Product "elementFormDefault="qualified"><import namespace=" http://www.example.org/Product " schemaLocation="Product.xsd"/><element name="quote"><complexType><sequence><element ref="prod:product"/></sequence></complexType></element> </schema>同樣,當我們從該XML模式生成類時,我們將引用從Product.xsd生成Java類時創建的情節文件。
xjc -d out ProductQuoteRequest.xsd -extension -b product.episode它是如何工作的? (product.episode)
對于你們中的那些人來說,這很奇怪。 XJC生成的情節文件實際上只是一個用于自定義類生成的標準JAXB綁定文件。 生成的綁定/情節文件包含一些條目,這些條目告訴XJC此類型的類已經存在。 您可以手工編寫此文件,但是XJC的-episode標志可以幫您完成。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb "> <!--This file was generated by the JavaTM Architecture for XML Binding (JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 See <a href=" http://java.sun.com/xml/jaxb "> http://java.sun.com/xml/jaxb </a> Any modifications to this file will be lost upon recompilation of the source schema. Generated on: 2011.11.02 at 03:40:10 PM EDT --> <bindings scd="x-schema::tns" xmlns:tns=" http://www.example.org/Product "> <schemaBindings map="false"/> <bindings scd="tns:product"> <class ref="org.example.product.Product"/> </bindings> </bindings> </bindings>參考: Java XML和JSON綁定博客中的JCG合作伙伴 Blaise Doughan 重用了生成的JAXB類 。
相關文章 :
- 使用JAXB從XSD生成XML
- 將對象映射到多個XML模式–天氣示例
翻譯自: https://www.javacodegeeks.com/2011/12/reusing-generated-jaxb-classes.html
jaxb 生成java類
總結
以上是生活随笔為你收集整理的jaxb 生成java类_重用生成的JAXB类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑怎么链接移动宽带(电脑怎么链接移动宽
- 下一篇: 如何优化Hibernate Elleme