Skip to main content

Using FastInfoset with JAXB

Posted by kohsuke on September 6, 2005 at 12:05 PM PDT

FastInfoset implementats SAX and StAX interfaces. This allows any XML technologies to be used with FastInfoset provided that they support either SAX or StAX. So it's generally that easy.

For example, the following code creates a StAX XMLStreamReader from a file encoded in the FastInfoset format:

import com.sun.xml.fastinfoset.stax.*;

InputStream in = new BufferedInputStream(new FileInputStream(...));
XMLStreamReader streamReader = new StAXDocumentParser(in);

So you can just pass this in to the unmarshaller to unmarshal this into JAXB objects:

import javax.xml.bind.*;

JAXBContext context = JAXBContext.newInstance(...);
Unmarshaller u = context.createUnmarshaller();
Object jaxbObject = u.unmarshal(streamReader);

Similarly, to marshal JAXB objects into FastInfoset, first create StAX XMLStreamWriter for FastInfoset like this:

import com.sun.xml.fastinfoset.stax.*;

BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(...));
StAXDocumentSerializer sw = new StAXDocumentSerializer();
sw.setOutputStream(fos);

Since StAXDocumentSerializer implements XMLStreamWriter, you can then just pass this writer to a JAXB marshaller:

import javax.xml.bind.*;

Marshaller m = context.createMarshaller();
m.marshal(jaxbObject,sw);

As I mentioned before, this technology works with any XML technologies, not just JAXB. So FastInfoset is really very easy to use.

That said, starting with a recent version of the JAXB RI, the RI internally recognizes that it's writing to FastInfoset, and it takes an optimized codepath just for FastInfoset. This gives you more performance boost without any work on your side. So it's not only fast, but it's also easy.

Related Topics >>

Comments

Error when using jaxb with FI

I am trying to use FI with JAXB as shown below:

public void marshall(T target, OutputStream outputStream)
throws MyException {


try {
StAXDocumentSerializer sw = new StAXDocumentSerializer();
sw.setOutputStream(outputStream);
Marshaller m = jaxbContext.createMarshaller();
XMLStreamWriter streamWriter = sw;
m.marshal(target, streamWriter);
} catch (JAXBException jaxbe) {
throw new MyException(jaxbe);
}

}


I get the following Exception:

java.lang.IncompatibleClassChangeError
at com.sun.xml.bind.v2.runtime.output.XMLStreamWriterOutput.startDocument(XMLStreamWriterOutput.java:101)
at com.sun.xml.bind.v2.runtime.XMLSerializer.startDocument(XMLSerializer.java:833)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.prewrite(MarshallerImpl.java:364)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:327)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:175)
at my.company.jaxb.JaxbHandler.marshall(JaxbHandler.java:265)
at my.company.ws.RequestHandler.serialize(RequestHandler.java:451)
at my.company.ws.RequestHandler.invoke(RequestHandler.java:127)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)


I am using Jaxb-2.1.5 and FastInfoset from Jaxws-2.2.

Looks like there is incompatibility in the class. Can you help me identify that?

thanks