Jaxb unmarshalls fixml object but all fields are null

Posted by DUFF on Stack Overflow See other posts from Stack Overflow or by DUFF
Published on 2012-10-28T22:57:46Z Indexed on 2012/10/28 23:00 UTC
Read the original article Hit count: 238

Filed under:

I have a small XML document in the FIXML format. I'm unmarshalling them using jaxb.

The problem

The process complete without errors but the objects which are created are completely null. Every field is empty. The fields which are lists (like the Qty) have the right number of object in them. But the fields of those objects are also null.

Setup

I've downloaded the FIXML schema from here and I've created the classes with xjc and the maven plugin. They are all in the package org.fixprotocol.fixml_5_0_sp2. I've got the sample xml in a file

FIXML.XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<FIXML>
<Batch>
    <PosRpt>
    <Pty ID="GS" R="22"/>
    <Pty ID="01" R="5"/>
    <Pty ID="6U8" R="28">
        <Sub ID="2" Typ="21"/>
    </Pty>
    <Pty ID="GS" R="22"/>
    <Pty ID="6U2" R="2"/>
    <Instrmt ID="GHPKRW" SecTyp="FWD" MMY="20121018" MatDt="2012-10-18" Mult="1" Exch="GS" PxQteCcy="KJS" FnlSettlCcy="GBP" Fctr="0.192233298" SettlMeth="G" ValMeth="FWDC2" UOM="Ccy" UOMCCy="USD">
        <Evnt EventTyp="121" Dt="2013-10-17"/>
        <Evnt EventTyp="13" Dt="2013-10-17"/>
    </Instrmt>
    <Qty Long="0.000" Short="22000000.000" Typ="PNTN"/>
    <Qty Long="0.000" Short="22000000.000" Typ="FIN"/>
    <Qty Typ="DLV" Long="0.00" Short="0.00" Net="0.0"/>
    <Amt Typ="FMTM" Amt="32.332" Ccy="USD"/>
    <Amt Typ="CASH" Amt="1" Rsn="3" Ccy="USD"/>
    <Amt Typ="IMTM" Amt="329.19" Ccy="USD"/>
    <Amt Typ="DLV" Amt="0.00" Ccy="USD"/>
    <Amt Typ="BANK" Amt="432.23" Ccy="USD"/>
</PosRpt>

Then I'm calling the unmarshaller with custom event handler which just throws an exception on a parse error. The parsing complete so I know there are no errors being generated. I'm also handling the namespace as suggested here

// sort out the file
String xmlFile = "C:\\FIXML.XML.xml";
System.out.println("Loading XML File..." + xmlFile);
InputStream input = new FileInputStream(xmlFile);
InputSource is = new InputSource(input);

// create jaxb context
JAXBContext jc = JAXBContext.newInstance("org.fixprotocol.fixml_5_0_sp2");
Unmarshaller unmarshaller = jc.createUnmarshaller();

// add event handler so jacB will fail on an error
CustomEventHandler validationEventHandler = new CustomEventHandler();
unmarshaller.setEventHandler(validationEventHandler);

// set the namespace
NamespaceFilter inFilter = new NamespaceFilter("http://www.fixprotocol.org/FIXML-5-0-SP2", true);

inFilter.setParent(SAXParserFactory.newInstance().newSAXParser().getXMLReader());
SAXSource source = new SAXSource(inFilter, is);

// GO!
JAXBElement<FIXML> fixml = unmarshaller.unmarshal(source, FIXML.class);

The fixml object is created. In the above sample the Amt array will have five element which matches the number of amts in the file. But all the fields like ccy are null. I've put breakpoints in the classes created by xjc and none of the setters are ever called.

So it appears that jaxb is unmarshalling and creating all the correct objects, but it's never calling the setters??

I'm completely stumped on this. I've seen a few posts that suggrest making sure the package.info file that was generated by xjc is in the packags and I've made sure that it's there. There are no working in the IDE about the generated code.

Any help much appreciated.

© Stack Overflow or respective owner

Related posts about jaxb