Menu

Sample code to demonstrate efficient JaXB unmarshalling

Help
Gordon
2014-03-19
2014-03-19
  • Gordon

    Gordon - 2014-03-19

    I'm not seeing a way to use the OpenEXI API to hand data to JaXB's unmarshaller (javax.xml.bind.Unmarshaller) without first decoding the binary data to an XML string via the transmogrifier. It seems that it'd be much more efficient to hand Jaxb an XMLStreamReader that decodes the EXI-formatted binary data on-the-fly.

    What am I missing? Is this doable via the OpenEXI API? Will you please supply some sample code to demonstrate this?

     
  • Gordon

    Gordon - 2014-03-19

    I think I've figured it out.

    Initially I was getting an EOFException due to having ported a ByteArrayOutputStream's contents to a ByteArrayInputStream via a toString() method which is a bad idea.

    Here's the code that appears to do what I'm looking for:

      System.out.println("Binary -> Jaxb starting");
      System.out.println("binaryStream length: " + binaryEncoderOutputStream.toString().length());
      GrammarCache grammarCache2 = new GrammarCache((EXISchema)null, GrammarOptions.DEFAULT_OPTIONS);
      EXIReader exiReader = new EXIReader();
      exiReader.setGrammarCache(grammarCache2);
    
      binaryEncoderOutputStream.flush();
      InputStream inputStream = new ByteArrayInputStream(binaryEncoderOutputStream.toByteArray());
      Unmarshaller unmarshaller = gotForDataGridJaxbContext.createUnmarshaller();
    
      JAXBElement<GetTrainForDataGridReplyType> jaxbObject
        = (JAXBElement<GetTrainForDataGridReplyType>) unmarshaller.unmarshal(new SAXSource(exiReader, new InputSource(inputStream)));
    
      inputStream.close();
    

    I'm attaching the whole method to give future readers the full context.

     

    Last edit: Gordon 2014-03-19

Log in to post a comment.