gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:43:27 25 4
gpt4 key购买 nike

有许多通过 XSLT 将 XML 流式传输 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 实现类。当然可以通过公共(public) JAXB 接口(interface)来完成,对吧?

谢谢!

最佳答案

您可以通过创建 XMLFilter 来访问 unmarshaller.unmarshal(source, type) 方法。此代码将允许您运行转换并解码为未绑定(bind)到 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有关处理器的更多信息。

关于java - 通过转换将 XML 流式传输到 JAXB Unmarshaller,并为根元素声明类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8888264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com