嵌套项的空值在JAXB / MOXY中解组

时间:2015-02-25 19:59:50

标签: java xml jaxb moxy

JAXB正在解析XML文档中每种类型遇到的第一个元素,但是将相同类型的任何嵌套元素的子元素保留为null。

简而言之,问题结构如下:

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

因此,剪贴画也可能有一个嵌套序列,整个循环可以递归重复。

遇到的第一个序列始终正确设置了媒体。

但是,对于嵌套序列,media元素始终为null(即使它是XML格式)

使用XSD中的xjc生成类。

为什么unmarshall不喜欢嵌套元素?除了嵌套元素,它一切正常。注释嵌套元素有什么特别之处吗?代码和注释非常简单。

任何线索都会受到赞赏。 谢谢, 约翰

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;

XML非常庞大,但这里有一个片段,序列中的媒体在不应该是的时候为空。

    <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 个答案:

答案 0 :(得分:0)

您的dom对象与xml不匹配。例如,序列将uuid元素作为第一个,而bean将其作为最后一个,&#34; updatebehavior&#34;完全迷失了。 与其他bean类似的问题。附加的xml文档根本不会对您的jaxb模型进行验证,这可能是它无法正确读取的原因。

从模型中获取架构:

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

在解组时验证模式:

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