简体   繁体   中英

Null values for nested items unmarshalling in JAXB/MOXY

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>

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM