-
- 所有已知实现类:
-
AbstractMarshallerImpl
public interface MarshallerMarshaller类负责管理将Java内容树序列化为XML数据的过程。 它提供了基本的编组方法:假设以下所有代码片段的设置代码:
JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); Unmarshaller u = jc.createUnmarshaller(); Object element = u.unmarshal( new File( "foo.xml" ) ); Marshaller m = jc.createMarshaller();编组文件:
OutputStream os = new FileOutputStream( "nosferatu.xml" ); m.marshal( element, os );编组到SAX ContentHandler:
// assume MyContentHandler instanceof ContentHandler m.marshal( element, new MyContentHandler() );编组到DOM节点:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); m.marshal( element, doc );编组到java.io.OutputStream:
m.marshal( element, System.out );编组到java.io.Writer:
m.marshal( element, new PrintWriter( System.out ) );编组到javax.xml.transform.SAXResult:
// assume MyContentHandler instanceof ContentHandler SAXResult result = new SAXResult( new MyContentHandler() ); m.marshal( element, result );编组到javax.xml.transform.DOMResult:
DOMResult result = new DOMResult(); m.marshal( element, result );编组到javax.xml.transform.StreamResult:
StreamResult result = new StreamResult( System.out ); m.marshal( element, result );编组到javax.xml.stream.XMLStreamWriter:
XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter( ... ); m.marshal( element, xmlStreamWriter );编组到javax.xml.stream.XMLEventWriter:
XMLEventWriter xmlEventWriter = XMLOutputFactory.newInstance().createXMLEventWriter( ... ); m.marshal( element, xmlEventWriter );The first parameter of the overloaded
Marshaller.marshal(java.lang.Object, ...)methods must be a JAXB element as computed byJAXBIntrospector.isElement(java.lang.Object); otherwise, aMarshaller.marshalmethod must throw aMarshalException. There exist two mechanisms to enable marshalling an instance that is not a JAXB element. One method is to wrap the instance as a value of aJAXBElement, and pass the wrapper element as the first parameter to aMarshaller.marshalmethod. For java to schema binding, it is also possible to simply annotate the instance's class with @XmlRootElement.编码
By default, the Marshaller will use UTF-8 encoding when generating XML data to a
java.io.OutputStream, or ajava.io.Writer. Use thesetPropertyAPI to change the output encoding used during these marshal operations. Client applications are expected to supply a valid character encoding name as defined in the W3C XML 1.0 Recommendation and supported by your Java Platform.验证和成型
Client applications are not required to validate the Java content tree prior to calling any of the marshal API's. Furthermore, there is no requirement that the Java content tree be valid with respect to its original schema in order to marshal it back into XML data. Different JAXB Providers will support marshalling invalid Java content trees at varying levels, however all JAXB Providers must be able to marshal a valid content tree back to XML data. A JAXB Provider must throw a
MarshalExceptionwhen it is unable to complete the marshal operation due to invalid content. Some JAXB Providers will fully allow marshalling invalid content, others will fail on the first validation error.Even when schema validation is not explictly enabled for the marshal operation, it is possible that certain types of validation events will be detected during the operation. Validation events will be reported to the registered event handler. If the client application has not registered an event handler prior to invoking one of the marshal API's, then events will be delivered to a default event handler which will terminate the marshal operation after encountering the first error or fatal error. Note that for JAXB 2.0 and later versions,
DefaultValidationEventHandleris no longer used.All JAXB Providers are required to support the following set of properties. Some providers may support additional properties.
-
jaxb.encoding- value must be a java.lang.String - The output encoding to use when marshalling the XML data. The Marshaller will use "UTF-8" by default if this property is not specified.
-
jaxb.formatted.output- value must be a java.lang.Boolean - This property controls whether or not the Marshaller will format the resulting XML data with line breaks and indentation. A true value for this property indicates human readable indented xml data, while a false value indicates unformatted xml data. The Marshaller will default to false (unformatted) if this property is not specified.
-
jaxb.schemaLocation- value must be a java.lang.String - This property allows the client application to specify an xsi:schemaLocation attribute in the generated XML data. The format of the schemaLocation attribute value is discussed in an easy to understand, non-normative form in Section 5.6 of the W3C XML Schema Part 0: Primer and specified in Section 2.6 of the W3C XML Schema Part 1: Structures.
-
jaxb.noNamespaceSchemaLocation- value must be a java.lang.String - This property allows the client application to specify an xsi:noNamespaceSchemaLocation attribute in the generated XML data. The format of the schemaLocation attribute value is discussed in an easy to understand, non-normative form in Section 5.6 of the W3C XML Schema Part 0: Primer and specified in Section 2.6 of the W3C XML Schema Part 1: Structures.
-
jaxb.fragment- value must be a java.lang.Boolean -
This property determines whether or not document level events will be generated by the Marshaller. If the property is not specified, the default is
false. This property has different implications depending on which marshal api you are using - when this property is set to true:
marshal(Object,ContentHandler)- the Marshaller won't invokeContentHandler.startDocument()andContentHandler.endDocument().marshal(Object,Node)- the property has no effect on this API.marshal(Object,OutputStream)- the Marshaller won't generate an xml declaration.marshal(Object,Writer)- the Marshaller won't generate an xml declaration.marshal(Object,Result)- depends on the kind of Result object, see semantics for Node, ContentHandler, and Stream APIsmarshal(Object,XMLEventWriter)- the Marshaller will not generateXMLStreamConstants.START_DOCUMENTandXMLStreamConstants.END_DOCUMENTevents.marshal(Object,XMLStreamWriter)- the Marshaller will not generateXMLStreamConstants.START_DOCUMENTandXMLStreamConstants.END_DOCUMENTevents.
"The
Marshallerprovides two styles of callback mechanisms that allow application specific processing during key points in the unmarshalling process. In 'class defined' event callbacks, application specific code placed in JAXB mapped classes is triggered during marshalling. 'External listeners' allow for centralized processing of marshal events in one callback method rather than by type event callbacks.Class defined event callback methods allow any JAXB mapped class to specify its own specific callback methods by defining methods with the following method signatures:
The class defined event callback methods should be used when the callback method requires access to non-public methods and/or fields of the class.// Invoked by Marshaller after it has created an instance of this object. boolean beforeMarshal(Marshaller); // Invoked by Marshaller after it has marshalled all properties of this object. void afterMarshal(Marshaller);
The external listener callback mechanism enables the registration of a
Marshaller.Listenerinstance with asetListener(Listener). The external listener receives all callback events, allowing for more centralized processing than per class defined callback methods.The 'class defined' and external listener event callback methods are independent of each other, both can be called for one event. The invocation ordering when both listener callback methods exist is defined in
Marshaller.Listener.beforeMarshal(Object)andMarshaller.Listener.afterMarshal(Object).An event callback method throwing an exception terminates the current marshal process.
- 从以下版本开始:
- 1.6,JAXB 1.0
- 另请参见:
-
JAXBContext,Validator,Unmarshaller
-
-
Nested Class Summary
Nested Classes Modifier and Type 接口 描述 static classMarshaller.Listener通过一个Marshaller注册这个类的实现的实例来外部监听元帅事件。
-
Field Summary
Fields Modifier and Type Field 描述 static StringJAXB_ENCODING用于在编组的XML数据中指定输出编码的属性的名称。static StringJAXB_FORMATTED_OUTPUT用于指定编组的XML数据是否使用换行符和缩进格式化的属性的名称。static StringJAXB_FRAGMENT用于指定编组器是否生成文档级事件(即调用startDocument或endDocument)的属性的名称。static StringJAXB_NO_NAMESPACE_SCHEMA_LOCATION用于指定要排列在编组XML输出中的xsi:noNamespaceSchemaLocation属性值的属性名称。static StringJAXB_SCHEMA_LOCATION用于指定要排列在编组XML输出中的xsi:schemaLocation属性值的属性名称。
-
方法摘要
所有方法 接口方法 抽象方法 Modifier and Type 方法 描述 <A extends XmlAdapter>
AgetAdapter(Class<A> type)获取与指定类型相关联的适配器。AttachmentMarshallergetAttachmentMarshaller()ValidationEventHandlergetEventHandler()返回当前事件处理程序或默认事件处理程序(如果尚未设置)。Marshaller.ListenergetListener()NodegetNode(Object contentTree)获取内容树的DOM树视图(可选)。ObjectgetProperty(String name)获取底层实现中的特定属性Marshaller。SchemagetSchema()获取用于执行组织时间验证的JAXP 1.3Schema对象。voidmarshal(Object jaxbElement, File output)将根据jaxbElement的内容树jaxbElement到一个文件中。voidmarshal(Object jaxbElement, OutputStream os)将根据jaxbElement的内容树jaxbElement成输出流。voidmarshal(Object jaxbElement, Writer writer)将根据jaxbElement的内容树jaxbElement成一个作家。voidmarshal(Object jaxbElement, XMLEventWriter writer)将根据jaxbElement的内容树jaxbElement成一个XMLEventWriter。voidmarshal(Object jaxbElement, XMLStreamWriter writer)将根据jaxbElement的内容树jaxbElement成一个XMLStreamWriter。voidmarshal(Object jaxbElement, Result result)将根据jaxbElement的内容树jaxbElement到指定的javax.xml.transform.Result。voidmarshal(Object jaxbElement, Node node)将根据jaxbElement的内容树jaxbElement到DOM树中。voidmarshal(Object jaxbElement, ContentHandler handler)将根据jaxbElement的内容树jaxbElement成SAX2事件。<A extends XmlAdapter>
voidsetAdapter(Class<A> type, A adapter)将配置的XmlAdapter实例与此编组相关联。voidsetAdapter(XmlAdapter adapter)将配置的XmlAdapter实例与此编组相关联。voidsetAttachmentMarshaller(AttachmentMarshaller am)关联使XML文档中的二进制数据能够以XML二进制优化的附件传输的上下文。voidsetEventHandler(ValidationEventHandler handler)允许应用程序注册验证事件处理程序。voidsetListener(Marshaller.Listener listener)注册元帅事件回调Marshaller.Listener与这Marshaller。voidsetProperty(String name, Object value)在底层实现中设置特定属性Marshaller。voidsetSchema(Schema schema)指定应用于验证后续元组操作的JAXP 1.3Schema对象。
-
-
-
字段详细信息
-
JAXB_ENCODING
static final String JAXB_ENCODING
用于在编组的XML数据中指定输出编码的属性的名称。- 另请参见:
- Constant Field Values
-
JAXB_FORMATTED_OUTPUT
static final String JAXB_FORMATTED_OUTPUT
用于指定编组的XML数据是否使用换行符和缩进格式化的属性的名称。- 另请参见:
- Constant Field Values
-
JAXB_SCHEMA_LOCATION
static final String JAXB_SCHEMA_LOCATION
用于指定要排列在编组XML输出中的xsi:schemaLocation属性值的属性名称。- 另请参见:
- Constant Field Values
-
JAXB_NO_NAMESPACE_SCHEMA_LOCATION
static final String JAXB_NO_NAMESPACE_SCHEMA_LOCATION
用于指定要排列在编组XML输出中的xsi:noNamespaceSchemaLocation属性值的属性名称。- 另请参见:
- Constant Field Values
-
JAXB_FRAGMENT
static final String JAXB_FRAGMENT
用于指定编组器是否生成文档级事件(即调用startDocument或endDocument)的属性的名称。- 另请参见:
- Constant Field Values
-
-
方法详细信息
-
marshal
void marshal(Object jaxbElement, Result result) throws JAXBException
将根据jaxbElement的内容树jaxbElement到指定的javax.xml.transform.Result。所有JAXB提供者必须至少支持
DOMResult,SAXResult,并StreamResult。 它也可以支持Result其他派生类。- 参数
-
jaxbElement- 要编组的内容树的根。 -
result- 将会将XML发送到此结果 - 异常
-
JAXBException- 如果在编组过程中发生意外问题。 -
MarshalException- 如果ValidationEventHandler从其handleEvent方法返回false,或Marshaller无法编组jaxbElement(或任何可从jaxbElement对象)。 见Marshalling a JAXB element 。 -
IllegalArgumentException- 如果任何方法参数为空
-
marshal
void marshal(Object jaxbElement, OutputStream os) throws JAXBException
将根据jaxbElement的内容树jaxbElement到输出流中。- 参数
-
jaxbElement- 要编组的内容树的根。 -
os- XML将添加到此流。 - 异常
-
JAXBException- 如果在编组过程中发生意外问题。 -
MarshalException- 如果ValidationEventHandler从其handleEvent方法返回false,或者Marshaller无法编组jaxbElement(或任何可从jaxbElement可访问的对象)。 见Marshalling a JAXB element 。 -
IllegalArgumentException- 如果任何方法参数为空
-
marshal
void marshal(Object jaxbElement, File output) throws JAXBException
将根据jaxbElement的内容树jaxbElement到一个文件中。- 参数
-
jaxbElement- 要编组的内容树的根。 -
output- 要写入的文件。 如果这个文件已经存在,它将被覆盖。 - 异常
-
JAXBException- 如果在编组过程中发生意外问题。 -
MarshalException- 如果ValidationEventHandler从其handleEvent方法返回false,或者Marshaller无法编组jaxbElement(或任何可从jaxbElement可访问的对象)。 参见Marshalling a JAXB element 。 -
IllegalArgumentException- 如果任何方法参数为空 - 从以下版本开始:
- 1.6,JAXB 2.1
-
marshal
void marshal(Object jaxbElement, Writer writer) throws JAXBException
将根据jaxbElement的内容树jaxbElement成一个Writer。- 参数
-
jaxbElement- 要编组的内容树的根。 -
writer- 将XML发送给作者。 - 异常
-
JAXBException- 如果在编组过程中出现意外问题。 -
MarshalException- 如果ValidationEventHandler从其handleEvent方法返回false,或者Marshaller无法编组jaxbElement(或任何可从jaxbElement可访问的对象)。 见Marshalling a JAXB element 。 -
IllegalArgumentException- 如果任何方法参数为空
-
marshal
void marshal(Object jaxbElement, ContentHandler handler) throws JAXBException
将根据jaxbElement的内容树jaxbElement成SAX2事件。- 参数
-
jaxbElement- 要编组的内容树的根。 -
handler- XML将作为SAX2事件发送到此处理程序。 - 异常
-
JAXBException- 如果在编组过程中发生意外问题。 -
MarshalException- 如果ValidationEventHandler从其handleEvent方法返回虚假,或者Marshaller无法编组jaxbElement(或任何可从jaxbElement可访问的对象)。 见Marshalling a JAXB element 。 -
IllegalArgumentException- 如果任何方法参数为空
-
marshal
void marshal(Object jaxbElement, Node node) throws JAXBException
将根据jaxbElement的内容树jaxbElement到DOM树中。- 参数
-
jaxbElement- 要编组的内容树。 -
node- 将作为该节点的子节点添加DOM节点。 此参数必须是一个可接受子(节点Document,DocumentFragment,或Element) - 异常
-
JAXBException- 如果编组期间出现意外问题。 -
MarshalException- 如果ValidationEventHandler从其handleEvent方法返回false,或者Marshaller无法编组jaxbElement(或任何可从jaxbElement对象)。 见Marshalling a JAXB element 。 -
IllegalArgumentException- 如果任何方法参数为空
-
marshal
void marshal(Object jaxbElement, XMLStreamWriter writer) throws JAXBException
将根源于jaxbElement的内容树jaxbElement成一个XMLStreamWriter。- 参数
-
jaxbElement- 要编组的内容树。 -
writer- XML将被发送给作者。 - 异常
-
JAXBException- 如果在编组过程中出现意外问题。 -
MarshalException- 如果ValidationEventHandler从其handleEvent方法返回false,或者Marshaller无法编组jaxbElement(或任何可从jaxbElement可访问的对象)。 见Marshalling a JAXB element 。 -
IllegalArgumentException- 如果任何方法参数为空 - 从以下版本开始:
- 1.6,JAXB 2.0
-
marshal
void marshal(Object jaxbElement, XMLEventWriter writer) throws JAXBException
将根据jaxbElement的内容树jaxbElement成一个XMLEventWriter。- 参数
-
jaxbElement- 根据要编组的jaxbElement的内容树。 -
writer- XML将发送给作者。 - 异常
-
JAXBException- 如果在编组过程中发生意外问题。 -
MarshalException- 如果ValidationEventHandler从其handleEvent方法返回false,或者Marshaller无法编组jaxbElement(或任何可从jaxbElement对象)。 见Marshalling a JAXB element 。 -
IllegalArgumentException- 如果任何方法参数为空 - 从以下版本开始:
- 1.6,JAXB 2.0
-
getNode
Node getNode(Object contentTree) throws JAXBException
- 参数
-
contentTree- JAXB XML内容的Java表示 - 结果
- contentTree的DOM树视图
- 异常
-
UnsupportedOperationException- 如果JAXB提供程序实现不支持内容树的DOM视图 -
IllegalArgumentException- 如果任何方法参数为空 -
JAXBException- 如果发生意外问题
-
setProperty
void setProperty(String name, Object value) throws PropertyException
在底层实现中设置特定属性Marshaller。 此方法只能用于设置上述标准JAXB定义属性之一或提供者特定属性。 尝试设置未定义的属性将导致抛出PropertyException。 见Supported Properties 。- 参数
-
name- 要设置的属性的名称。 可以使用常数字段或用户提供的字符串指定此值。 -
value- 要设置的属性的值 - 异常
-
PropertyException- 处理给定属性或值时出错 -
IllegalArgumentException- 如果name参数为null
-
getProperty
Object getProperty(String name) throws PropertyException
获取底层实现中的特定属性Marshaller。 此方法只能用于获取上述标准JAXB定义的属性之一或提供者特定属性。 尝试获取未定义的属性将导致抛出PropertyException。 见Supported Properties 。- 参数
-
name- 要检索的属性的名称 - 结果
- 请求的属性的值
- 异常
-
PropertyException- 检索给定属性或值属性名称时出现错误 -
IllegalArgumentException- 如果name参数为null
-
setEventHandler
void setEventHandler(ValidationEventHandler handler) throws JAXBException
允许应用程序注册验证事件处理程序。如果在任何元帅API的调用期间遇到任何验证错误,JAXB提供者将调用验证事件处理程序。 如果客户端应用程序在调用其中一个元数据方法之前没有注册验证事件处理程序,则验证事件将由默认事件处理程序处理,该事件处理程序将在遇到第一个错误或致命错误后终止元组操作。
使用null参数调用此方法将导致Marshaller恢复为默认的默认事件处理程序。
- 参数
-
handler- 验证事件处理程序 - 异常
-
JAXBException- 如果在设置事件处理程序时遇到错误
-
getEventHandler
ValidationEventHandler getEventHandler() throws JAXBException
返回当前事件处理程序或默认事件处理程序(如果尚未设置)。- 结果
- 当前的ValidationEventHandler或默认事件处理程序(如果尚未设置)
- 异常
-
JAXBException- 如果在获取当前事件处理程序时遇到错误
-
setAdapter
void setAdapter(XmlAdapter adapter)
将配置的XmlAdapter实例与此编组相关联。这是调用
setAdapter(adapter.getClass(),adapter);的方便方法。- 异常
-
IllegalArgumentException- 如果适配器参数为空。 -
UnsupportedOperationException- 如果UnsupportedOperationException调用JAXB 1.0实现。 - 从以下版本开始:
- 1.6,JAXB 2.0
- 另请参见:
-
setAdapter(Class,XmlAdapter)
-
setAdapter
<A extends XmlAdapter> void setAdapter(Class<A> type, A adapter)
将配置的XmlAdapter实例与此编组相关联。每编组内部维护一个
Map<Class,XmlAdapter>,它使用用于编组类,它们的字段/方法的注解为XmlJavaTypeAdapter。此方法允许应用程序使用配置的实例
XmlAdapter。 当没有给出适配器的实例时,编组者将通过调用其默认构造函数来创建一个。- 参数
-
type- 适配器的类型。 当XmlJavaTypeAdapter.value()引用此类型时,将使用指定的实例。 -
adapter- 要使用的适配器的实例。 如果为空,它将取消注册此类型的当前适配器集。 - 异常
-
IllegalArgumentException- 如果type参数为null。 -
UnsupportedOperationException- 如果UnsupportedOperationException调用JAXB 1.0实现。 - 从以下版本开始:
- 1.6,JAXB 2.0
-
getAdapter
<A extends XmlAdapter> A getAdapter(Class<A> type)
获取与指定类型相关联的适配器。 这是setAdapter(javax.xml.bind.annotation.adapters.XmlAdapter)方法的反向操作。- 异常
-
IllegalArgumentException- 如果type参数为null。 -
UnsupportedOperationException- 如果UnsupportedOperationException调用JAXB 1.0实现。 - 从以下版本开始:
- 1.6,JAXB 2.0
-
setAttachmentMarshaller
void setAttachmentMarshaller(AttachmentMarshaller am)
关联使XML文档中的二进制数据能够以XML二进制优化的附件传输的上下文。 附件由XML文档内容模型通过存储在xml文档中的content-id URI(cid)引用引用。
- 异常
-
IllegalStateException- 如果在组织操作期间尝试同时调用此方法。
-
getAttachmentMarshaller
AttachmentMarshaller getAttachmentMarshaller()
-
setSchema
void setSchema(Schema schema)
- 参数
-
schema- 验证组织操作的模式对象或者禁用验证的null - 异常
-
UnsupportedOperationException- 如果在引用JAXB 1.0映射类的JAXBContext创建的Marshaller上调用此方法,则可能会抛出 - 从以下版本开始:
- 1.6,JAXB 2.0
-
getSchema
Schema getSchema()
获取用于执行组织时间验证的JAXP 1.3Schema对象。 如果在编组器中没有设置Schema,则该方法将返回null,表示将不执行组织时间验证。- 结果
- 用于执行组织时间验证的Schema对象,如果不存在则使用null。
- 异常
-
UnsupportedOperationException- 如果在引用JAXB 1.0映射类的JAXBContext创建的Marshaller上调用此方法,则可能会抛出 - 从以下版本开始:
- 1.6,JAXB 2.0
-
setListener
void setListener(Marshaller.Listener listener)
注册元帅事件回调
Marshaller.Listener与这Marshaller。每个Marshaller只有一个监听器。 设置侦听器将替换以前设置的侦听器。 可以通过将侦听器设置为
null来取消注册当前侦听器。- 参数
-
listener- 实现Marshaller.Listener的类的实例 - 从以下版本开始:
- 1.6,JAXB 2.0
-
getListener
Marshaller.Listener getListener()
- 结果
-
注册
Marshaller.Listener或null如果没有监听器注册此Marshaller。 - 从以下版本开始:
- 1.6,JAXB 2.0
-
-