【问题标题】:Streaming XML through Transformation into JAXB Unmarshaller, with a declared class type for the root element通过转换将 XML 流式传输到 JAXB Unmarshaller,并为根元素声明类类型
【发布时间】:2012-02-11 21:09:14
【问题描述】:

有许多通过 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();

还有这样的 declared type 的 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 接口来实现,对吧?

谢谢!

【问题讨论】:

    标签: java jaxb


    【解决方案1】:

    您可以通过创建 XMLFilter 来访问 unmarshaller.unmarshal(source, type) 方法。此代码将允许您运行转换并解组到未绑定到 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      相关资源
      最近更新 更多