java - Streaming XML through Transformation into JAXB Unmarshaller, with a declared class type for the root element -
java - Streaming XML through Transformation into JAXB Unmarshaller, with a declared class type for the root element -
there many examples of streaming xml through xslt, jaxb java objects. this:
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();
there examples of jaxb unmarshal declared type (from 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 // local element declaration in schema. // footype jaxb mapping of type of local element declaration foo. jaxbelement<footype> foo = u.unmarshal(foosubtree, footype.class);
note how specifying footype.class root element in u.unmarshal(foosubtree, footype.class)
call. nice.
the question is: there way combine streaming way of processing in top illustration way of specifying declared type in illustration below?
i fond way of achieving it, requires accessing jaxb implementation classes. certainly possible via public jaxb interface, right?
thank you!
you can access unmarshaller.unmarshal(source, type) methods creating xmlfilter. code allow run transform , unmarshal object not bound root element in 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; seek { // transform , source streams. transformin = ...; sourcein = ...; // create filtered source saxtransformerfactory mill = (saxtransformerfactory)transformerfactory.newinstance(); xmlfilter filter = factory.newxmlfilter(new streamsource(transformin)); source source = new saxsource(filter, new inputsource(new inputstreamreader(sourcein, "utf-8"))); // unmarshal object. unmarshaller jaxbunmarshaller = jaxbcontext.newinstance(objectfactory.class.getpackage().getname()).createunmarshaller(); jaxbelement<footype> foo = jaxbunmarshaller.unmarshal(source, footype.class); } { closequietly(transformin); closequietly(sourcein); }
warning: because using streaming api not mean implementation streaming. xslt processors build dom style representation of input before transforming document. if trying avoid building document before unmarshalling, code may not that. see this answer more info processors.
java jaxb
Comments
Post a Comment