參數屬性:
schemagen支持javac定義的大多數屬性。并且支持下面的參數屬性:
destdir:生成的schema文件的基準目錄。
classpath:和子元素<classpath>功能相同。
schemagen支持所有的javac任務(wù)定義的子元素。
schema:控制生成的schema文件的名字。這個(gè)元素必須設置namespace屬性和file屬性。當這個(gè)元素存在的時(shí)候,指定的namespace的schema文件的名字和file屬性指定的名字相同。文件名和destdir屬性指定的文件名是相對的。當不存在destdir時(shí),將項目basedir作為destdir??梢远啻卧O置schema元素。
classpath:和路徑類(lèi)似的結構來(lái)表示classpath。
下面是一些schemagen生成器的例子:
將src目錄下的源文件生成schema文件,保存在build/schemas目錄。
<schemagen srcdir="src" destdir="build/schemas">
使用javac的子元素:
<schemagen destdir="build/schemas">
<src path="src" />
<exclude name="Main.java"/>
</schemagen>
6. JAXB RI Extensions 運行時(shí)屬性:
JAXB RI提供了一些在JAXB規范中沒(méi)有定義的額外的Marshaller屬性,這些屬性可以更好的控制marshalling過(guò)程。但是,這只適用于JAXB RI,不一定適用于其他的JAXB提供者。
名稱(chēng)前綴映射
屬性名:com.sun.xml.bind.namespacePrefixMapper
類(lèi)型:com.sun.xml.bind.marshaller.NamespacePrefixMapper
默認值:無(wú)
JAXB RI提供了一個(gè)方法來(lái)將URI名稱(chēng)空間映射到前綴。下面是通常的過(guò)程:
應用程序開(kāi)發(fā)人員提供一個(gè)com.sun.xml.bind.marshaller.NamespacePrefixMapper的實(shí)現
接著(zhù)將這個(gè)類(lèi)設置為RI指定的com.sun.xml.bind.namespacePrefixMapper屬性
每當marshaller遇到一個(gè)URI,都會(huì )回調映射
當mapper返回值時(shí),marshaller就會(huì )使用這個(gè)值
com.sun.xml.bind.marshaller.NamespacePrefixMapper類(lèi)需要實(shí)現下面的方法:
public abstract class NamespacePrefixMapper {
/**
* Returns a preferred prefix for the given namespace URI.
*
* This method is intended to be overrided by a derived class.
*
* @param namespaceUri
* The namespace URI for which the prefix needs to be found.
* Never be null. "" is used to denote the default namespace.
* @param suggestion
* When the content tree has a suggestion for the prefix
* to the given namespaceUri, that suggestion is passed as a
* parameter. Typically this value comes from QName.getPrefix()
* to show the preference of the content tree. This parameter
* may be null, and this parameter may represent an already
* occupied prefix.
* @param requirePrefix
* If this method is expected to return non-empty prefix.
* When this flag is true, it means that the given namespace URI
* cannot be set as the default namespace.
*
* @return
* null if there''s no preferred prefix for the namespace URI.
* In this case, the system will generate a prefix for you.
*
* Otherwise the system will try to use the returned prefix,
* but generally there''s no guarantee if the prefix will be
* actually used or not.
*
* return "" to map this namespace URI to the default namespace.
* Again, there''s no guarantee that this preference will be
* honored.
*
* If this method returns "" when requirePrefix=true, the return
* value will be ignored and the system will generate one.
*/
public abstract String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix);
}
如果這個(gè)屬性被設置為null的話(huà),下面默認的屬性就會(huì )返回:
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
return suggestion;
}
縮進(jìn)
屬性名:com.sun.xml.bind.indentString
類(lèi)型:java.lang.String
默認值:” ”四個(gè)空格
這個(gè)屬性用來(lái)控制XML的縮進(jìn)。
注意需要設置jaxb.formatted.output屬性來(lái)啟用縮進(jìn)。
轉義字符
屬性名:com.sun.xml.bind.characterEscapeHandler
類(lèi)型:com.sun.xml.bind.marshaller.CharacterEscapeHandler
默認值:null
默認情況下,JAXB RI的實(shí)現會(huì )試著(zhù)轉義字符,這樣的話(huà),就可以安全的表示輸出編碼。
不過(guò),由于各種技術(shù)原因,默認的操作可能不會(huì )滿(mǎn)足要求。如果需要處理更敏捷的處理轉義,那么可以按照如下過(guò)程進(jìn)行操作:
實(shí)現com.sun.xml.bind.marshaller.CharacterEscapeHandler接口
創(chuàng )建創(chuàng )建一個(gè)實(shí)例
將實(shí)例設置為marshaller的屬性
默認情況下的轉義是和J2SE SDK的版本相關(guān),如果運行J2SE SDK 版本1.3或者之前的版本不能滿(mǎn)足轉義需求的話(huà),可以使用版本1.4或者之后的版本。
XML聲明控制
屬性名:com.sun.xml.bind.xmlDeclaration
類(lèi)型:java.lang.Boolean
默認值:Boolean.TRUE
這是JAXB RI 1.0.x的實(shí)驗屬性,并且被JAXB 2.0標準所采用。2.0 RI會(huì )繼續支持這個(gè)屬性,但是客戶(hù)端的代碼應該使用Marshaller.JAXB_FRAGMENT屬性。
在JAXB 2.0中,調用:
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
和調用
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
是等價(jià)的。
啟用片段marshalling可以使得將一個(gè)片段輸出到另外一個(gè)XML的時(shí)候。同樣,當需要輸出類(lèi)似DOCTYPE聲明或者XML樣式表處理指示,參考如下的代碼:
PrintWriter out = ...;
// print out the prolog part by ourselves
out.println("<xml version=''1.0''?>");
out.println("<!DOCTYPE foo SYSTEM ''dummy.dtd''>");
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
marshaller.marshal( jaxbObject, out );
JAXB RI提供了JAXB規范沒(méi)有定義的自定義內容。
這些特性只有在-extension模式下運行綁定編譯器時(shí)才可以使用
所有JAXB RI供應商擴展都定義在
http://java.sun.com/xml/ns/jaxb/xjc名稱(chēng)空間
名稱(chēng)空間中包含了擴展綁定聲明,這將使得全局屬性@jaxb:extensionBindingPrefixes在<xs:schema>元素。這個(gè)屬性的值是以空格分隔的名稱(chēng)空間前綴。
擴展普通的超類(lèi)
<xjc:superClass>自定義屬性可以指定一個(gè)full qualified name作為Java類(lèi)名來(lái)作為所有的生成類(lèi)的超類(lèi)。<xjc:superClass>只可以在<jaxb:globalBindings>:
<xs:schema xmlns:xs="
xmlns:jaxb=" xmlns:xjc=" jaxb:extensionBindingPrefixes="xjc"
jaxb:version="1.0">
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings>
<xjc:superClass name="org.acme.RocketBooster"/>
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
.
.
.
</xs:schema>
還可以擴展共同的接口
和擴展共同的類(lèi)類(lèi)似,只是使用<xjc:superInterface>來(lái)代替<xjc:superClass>。
<xs:schema xmlns:xs=" xmlns:jaxb=" xmlns:xjc=" jaxb:extensionBindingPrefixes="xjc"
jaxb:version="1.0">
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings generateValueClass="false">
<xjc:superInterface name="org.acme.RocketBooster"/>
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
.
.
.
</xs:schema>
7. 關(guān)于Schema
JAXB RI目前支持下面所列舉的Schema語(yǔ)言。下面的一些特性并沒(méi)有在JAXB規范中描述,只是在JAXB RI中才是可用的。
W3C Schema:JAXB 2.0 RI FCS會(huì )100%的支持W3C XML Schema。但是,現在的版本只是一個(gè)臨時(shí)版本,還沒(méi)有實(shí)現全部的功能。本節描述了JAXB 2.0規范。Section 8 描述了Java to XML,Section 6和7描述了XML to Java。
RELAX NG:目前還沒(méi)有支持。
DTD:JAXB RI目前試著(zhù)支持DTD,但是不能保證沒(méi)有問(wèn)題??梢允褂孟旅娴墓ぞ邅?lái)編譯XML的DTD。
$xjc.sh –dtd test.dtd