Есть много примеров потоковой передачи XML через XSLT, а затем 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);
Обратите внимание, какмы указываем FooType.class для корневого элемента в вызове u.unmarshal(fooSubtree, FooType.class)
.Хорошо.
Вопрос в следующем: Есть ли способ объединить потоковый способ обработки, как в верхнем примере, со способом указания объявленного типа, как в примере ниже?
Мне нравится способ его достижения, но он требует доступа к классам реализации JAXB.Конечно, это возможно сделать через общедоступный интерфейс JAXB, верно?
Спасибо!