JAXBSource (Java EE 5)

Java EE API


javax.xml.bind.util Class JAXBSource

java.lang.Object
  extended by javax.xml.transform.sax.SAXSource
      extended by javax.xml.bind.util.JAXBSource
All Implemented Interfaces:
Source

public class JAXBSource
extends SAXSource

Extends: javax.xml.transform.sax.SAXSource

编组 JAXB 生成的对象的 JAXP javax.xml.transform.Source 实现。

此实用工具类对于将 JAXB 与其他 Java/XML 技术组合很有用。

以下示例显示了如何使用 JAXB 编组将由 XSLT 转换的文档。

MyObject o = // get JAXB content tree
       
// jaxbContext is a JAXBContext object from which 'o' is created.
JAXBSource source = new JAXBSource( jaxbContext, o );
       
// set up XSLT transformation
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
       
// run transformation
t.transform(source,new StreamResult(System.out));
    

SAXSource 派生于 JAXBSource 是一个实现细节。因此,通常应用程序最好不要访问 SAXSource 上定义的方法。尤其不能调用 setXMLReader 和 setInputSource 方法。通过 getXMLReader 方法获取的 XMLReader 对象只能用于解析 getInputSource 方法返回的 InputSource 对象。

同样,通过 getInputSource 方法获得的 InputSource 对象只能由 getXMLReader 返回的 XMLReader 对象来解析。

英文文档:

JAXP Source implementation that marshals a JAXB-generated object.

This utility class is useful to combine JAXB with other Java/XML technologies.

The following example shows how to use JAXB to marshal a document for transformation by XSLT.

       MyObject o = // get JAXB content tree
       
       // jaxbContext is a JAXBContext object from which 'o' is created.
       JAXBSource source = new JAXBSource( jaxbContext, o );
       
       // set up XSLT transformation
       TransformerFactory tf = TransformerFactory.newInstance();
       Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
       
       // run transformation
       t.transform(source,new StreamResult(System.out));
    

The fact that JAXBSource derives from SAXSource is an implementation detail. Thus in general applications are strongly discouraged from accessing methods defined on SAXSource. In particular, the setXMLReader and setInputSource methods shall never be called. The XMLReader object obtained by the getXMLReader method shall be used only for parsing the InputSource object returned by the getInputSource method.

Similarly the InputSource object obtained by the getInputSource method shall be used only for being parsed by the XMLReader object returned by the getXMLReader.

Author:
Kohsuke Kawaguchi ([email protected])

Field Summary
 
Fields inherited from class javax.xml.transform.sax.SAXSource
 
Constructor Summary
 
Method Summary
 
Methods inherited from class javax.xml.transform.sax.SAXSource
 
Methods inherited from class java.lang.Object
 

Constructor Detail

public JAXBSource(JAXBContext context, Object contentObject) throws JAXBException
针对给定的内容对象创建一个新 javax.xml.transform.Source
context 用于创建 contentObject 的 JAXBContext。此上下文用于创建 marshaller 的新实例,不得为 null。
contentObject JAXB 生成的类的实例,将用作 javax.xml.transform.Source(通过将它编组到 XML)。它不得为 null。
ThrowsJAXBException: 如果创建 JAXBSource 时遇到错误,或者任何一个参数为 null。
英文文档:

JAXBSource

public JAXBSource(JAXBContext context,
                  Object contentObject)
           throws JAXBException
Creates a new Source for the given content object.

Parameters:
context - JAXBContext that was used to create contentObject. This context is used to create a new instance of marshaller and must not be null.
contentObject - An instance of a JAXB-generated class, which will be used as a Source (by marshalling it into XML). It must not be null.
Throws:
JAXBException - if an error is encountered while creating the JAXBSource or if either of the parameters are null.


public JAXBSource(Marshaller marshaller, Object contentObject) throws JAXBException
针对给定的内容对象创建一个新 javax.xml.transform.Source
marshaller 用于将 contentObject 编组到 XML 的 marshaller 实例。此实例必须根据用来构建 contentObject 的 JAXBContext 创建,不得为 null。
contentObject JAXB 生成的类的实例,将用作 javax.xml.transform.Source(通过将它编组到 XML)。它不得为 null。
ThrowsJAXBException: 如果创建 JAXBSource 时遇到错误,或者任何一个参数为 null。
英文文档:

JAXBSource

public JAXBSource(Marshaller marshaller,
                  Object contentObject)
           throws JAXBException
Creates a new Source for the given content object.

Parameters:
marshaller - A marshaller instance that will be used to marshal contentObject into XML. This must be created from a JAXBContext that was used to build contentObject and must not be null.
contentObject - An instance of a JAXB-generated class, which will be used as a Source (by marshalling it into XML). It must not be null.
Throws:
JAXBException - if an error is encountered while creating the JAXBSource or if either of the parameters are null.



Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!