通过转换将XML流式传输到JAXB Unmarshaller,并为根元素声明了类类型

时间:2012-01-17 00:35:06

标签: java jaxb

通过XSLT有很多流式传输XML 的例子,然后是JAXB到Java对象的例子。通常他们看起来像这样:

Transformer responseTransformer = TransformerFactory.newInstance().newTransformer(new StreamSource(getClass().getResourceAsStream("ResponseTransformation.xsl")));
Unmarshaller jaxbUnmarshaller = JAXBContext.newInstance(ObjectFactory.class.getPackage().getName()).createUnmarshaller();

JAXBResult jaxbResult = new JAXBResult(jaxbUnmarshaller);
responseTransformer.transform(new StreamSource(new StringReader(responseXml)), jaxbResult);
res = jaxbResult.getResult();

还有像这样的声明类型的JAXB Unmarshal示例(来自Unmarshaller javadoc):

JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
Unmarshaller u = jc.createUnmarshaller();

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File( "nosferatu.xml"));
Element  fooSubtree = ...; // traverse DOM till reach xml element foo, constrained by a 
                           // local element declaration in schema.

// FooType is the JAXB mapping of the type of local element declaration foo.
JAXBElement<FooType> foo = u.unmarshal(fooSubtree, FooType.class);

注意我们如何在u.unmarshal(fooSubtree, FooType.class)调用中为根元素指定FooType.class。好的。

问题是:有没有办法将流式处理方式与顶部示例结合使用,并采用指定声明类型的方式,如下例所示?

我喜欢实现它的方法,但它需要访问JAXB实现类。当然可以通过公共JAXB接口来实现,对吧?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以通过创建XMLFilter来访问unmarshaller.unmarshal(源,类型)方法。此代码将允许您运行转换并解组为未绑定到XSD中的根元素的对象:

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLFilter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.JAXBResult;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamSource;
import static org.apache.commons.io.IOUtils.closeQuietly;
...
InputStream transformIn = null;
InputStream sourceIn = null;

try {
  // get the transform and source streams.
  transformIn = ...;
  sourceIn = ...;

  // create the filtered source
  SAXTransformerFactory factory = (SAXTransformerFactory)TransformerFactory.newInstance();
  XMLFilter filter = factory.newXMLFilter(new StreamSource(transformIn));
  Source source = new SAXSource(filter, new InputSource(new InputStreamReader(sourceIn, "UTF-8")));

  // unmarshal the object.
  Unmarshaller jaxbUnmarshaller = 
    JAXBContext.newInstance(ObjectFactory.class.getPackage().getName()).createUnmarshaller();
  JAXBElement<FooType> foo = jaxbUnmarshaller.unmarshal(source, FooType.class);
}
finally {
  closeQuietly(transformIn);
  closeQuietly(sourceIn);
}

警告:仅仅因为您使用的是流式传输API并不意味着实现是流式传输。大多数XSLT处理器将在转换文档之前构建输入的DOM样式表示。如果您在解组之前尝试避免构建文档,则此代码可能不会这样做。有关处理器的详细信息,请参阅this answer