在 JAXB/MOXY 中解组的嵌套项的空值
Null values for nested items unmarshalling in JAXB/MOXY
JAXB 正在对 XML 文档中遇到的每种类型的第一个元素进行解组,但将相同类型的任何嵌套元素的子元素留空。
简而言之,有问题的结构如下:
sequence->media->audio->track->clipitem(->sequence etc etc)
所以,一个clipitem也可能有一个嵌套序列,整个循环可以递归重复。
遇到的第一个序列总是正确设置媒体。
但是,对于嵌套序列,媒体元素始终为空(即使它在 XML 中)
类 是使用 xjc 从 XSD 生成的。
为什么解组不喜欢嵌套元素?除了嵌套元素外,一切正常。有什么特别的东西可以注释嵌套元素吗?代码和注释都非常简单。
如有任何线索,我们将不胜感激。
谢谢,
约翰
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 很大,但这里有一个片段,其中序列中的媒体在不应该为 null 的情况下为 null。
<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>
您的 dom 对象与 xml 不匹配。例如,序列的第一个是 uuid 元素,而您的 bean 的最后一个是它,"updatebehavior" 完全丢失。
其他豆子也有类似的问题。随附的 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"));
JAXB 正在对 XML 文档中遇到的每种类型的第一个元素进行解组,但将相同类型的任何嵌套元素的子元素留空。
简而言之,有问题的结构如下:
sequence->media->audio->track->clipitem(->sequence etc etc)
所以,一个clipitem也可能有一个嵌套序列,整个循环可以递归重复。
遇到的第一个序列总是正确设置媒体。
但是,对于嵌套序列,媒体元素始终为空(即使它在 XML 中)
类 是使用 xjc 从 XSD 生成的。
为什么解组不喜欢嵌套元素?除了嵌套元素外,一切正常。有什么特别的东西可以注释嵌套元素吗?代码和注释都非常简单。
如有任何线索,我们将不胜感激。 谢谢, 约翰
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 很大,但这里有一个片段,其中序列中的媒体在不应该为 null 的情况下为 null。
<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>
您的 dom 对象与 xml 不匹配。例如,序列的第一个是 uuid 元素,而您的 bean 的最后一个是它,"updatebehavior" 完全丢失。 其他豆子也有类似的问题。随附的 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"));