0
votes

JAXB is unmarshalling the first elements encountered of each type in an XML document fine but leaving the children of any nested elements of the same type null.

Briefly, the problematic structure is as follows:

sequence->media->audio->track->clipitem(->sequence etc etc)

So, a clipitem may also have a nested sequence, and the whole cycle can repeat recursively.

The first sequence encountered always has media set correctly.

However, for a nested sequence the media element is always null (even though it is in XML)

The classes were generated with xjc from an XSD.

Why would unmarshall not like nested elements? Apart from nesting elements it is all working fine. Is there something special to annotate nested elements? The code and annotations are pretty simple.

Any clues would be appreciated. Thanks, John

JAXBContext jc = JAXBContext.newInstance(Xmeml.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Xmeml xmeml = (Xmeml) unmarshaller.unmarshal(inFile);


@XmlRootElement(name = "sequence")
public class Sequence {

    @XmlElement(required = true)
    protected String name;
    @XmlElement(required = true)
    protected BigInteger duration;
    @XmlElement(required = true)
    protected Rate rate;
    @XmlElement(required = true)
    protected Timecode timecode;
    @XmlElement(required = true)
    protected BigInteger in;
    @XmlElement(required = true)
    protected BigInteger out;
    @XmlElement(required = true)
    protected Media media;



@XmlRootElement(name = "clipitem")
public class Clipitem {

    @XmlElement(required = true)
    protected String name;
    @XmlElement(required = true)
    protected BigInteger duration;
    @XmlElement(required = true)
    protected Rate rate;
    protected boolean enabled;
    @XmlElement(required = true)
    protected BigInteger in;
    @XmlElement(required = true)
    protected BigInteger out;
    @XmlElement(required = true)
    protected BigInteger start;
    @XmlElement(required = true)
    protected BigInteger end;
    @XmlElement(required = true)
    protected String masterclipid;
    protected boolean ismasterclip;
    @XmlElement(required = true)
    protected Labels labels;
    @XmlElement(required = true)
    protected Comments comments;
    @XmlElement(required = true)
    protected Sequence sequence;

The XML is huge but here is a snippet where media inside a sequence is null when it shouldn't be.

    <track>
        <clipitem id="Nested Sequence">
            <name>Nested Sequence</name>
            <duration>815</duration>
            <rate>
                <ntsc>FALSE</ntsc>
                <timebase>25</timebase>
            </rate>
            <in>0</in>
            <out>815</out>
            <start>815</start>
            <end>1630</end>
            <sequence id="Nested Sequence1">
                <name>Nested Sequence</name>
                <duration>815</duration>
                <rate>
                    <ntsc>FALSE</ntsc>
                    <timebase>25</timebase>
                </rate>
                <timecode>
                    <rate>
                        <ntsc>FALSE</ntsc>
                        <timebase>25</timebase>
                    </rate>
                    <string>01:00:00:00</string>
                    <frame>90000</frame>
                    <source>source</source>
                    <displayformat>NDF</displayformat>
                </timecode>
                <in>-1</in>
                <out>-1</out>
                <media>
                    <video>
                        <format>
                            <samplecharacteristics>
                                <width>1920</width>
                                <height>1080</height>
                                <anamorphic>FALSE</anamorphic>
                                <pixelaspectratio>NTSC-601</pixelaspectratio>
                                <fielddominance>lower</fielddominance>
                                <rate>
                                    <ntsc>FALSE</ntsc>
                                    <timebase>25</timebase>
                                </rate>
1
it looks okish, can you add the xml that is not being parsed correctly.Zielu
I added a snippet of the XML where media inside sequence is coming out null. The whole XML is massive so thought better not post it all.John Baker
Place somewhere your all beans and exmple of xml that does not unmarshall correctly (Not whole just the one failing branch is enough) and add a link to them. So we can test if the unmarshalling really does not work or is it somehting on your side. In principle it should work. Typical culprit will be namespace issues but you should rather get error messages not nulls.Zielu
Here is a zip with the beans, an xml and a testcase class mrlimbic.com/test/testcase.zipJohn Baker
In case it matters, I also have eclipselink 2.5.2 on the classpath.John Baker

1 Answers

0
votes

Your dom objects does not match the xml. For example the sequence has uuid element as first while your bean has it as last, the "updatebehavior" is lost completely. Similar problems with other beans. The attached xml document does not validate against your jaxb model at all, which is probably the reason why it is not read correctly.

To get the schema from your model:

JAXBContext con = JAXBContext.newInstance(Xmeml.class);
File dir = new File("D:\\Temp\\schema");
con.generateSchema(new MySchemaOutputResolver(dir));

class MySchemaOutputResolver extends SchemaOutputResolver {

    protected File baseDir;

    public MySchemaOutputResolver(File dir)
    {
        super();
        baseDir =  dir;
    }

    @Override
    public Result createOutput( String namespaceUri, String suggestedFileName ) throws IOException {
        return new StreamResult(new File(baseDir,suggestedFileName));
    }
}

to validate against the schema when unmarshalling:

JAXBContext con = JAXBContext.newInstance(Xmeml.class);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("D:\\Temp\\schema\\schema1.xsd"));
Unmarshaller umar = con.createUnmarshaller();
umar.setSchema(schema);
Xmeml mem = (Xmeml)umar.unmarshal(new File("D:\\Temp\\testcase\\Surround Test.xml"));