JAXB/MOXy—在单个类中解组子类和嵌套子类的值,包括对同名元素的支持

JAXB/MOXy—在单个类中解组子类和嵌套子类的值,包括对同名元素的支持,jaxb,unmarshalling,moxy,Jaxb,Unmarshalling,Moxy,我想将包含同名嵌套子类的xml文件解组到单个类。我尝试了我发现的一切,但没有任何效果,嵌套子项的值仍然为空 怎么了 jaxb.properties javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory myFile.xml <?xml version="1.0" encoding="UTF-8"?> <clock> <name>myClock</

我想将包含同名嵌套子类的xml文件解组到单个类。我尝试了我发现的一切,但没有任何效果,嵌套子项的值仍然为空

怎么了

jaxb.properties

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
myFile.xml

<?xml version="1.0" encoding="UTF-8"?>
<clock>
  <name>myClock</name>
  <times>
    <starttime>09:00</starttime>
    <endtime>12:00</endtime>
    <starttime>13:00</starttime>
    <endtime>17:00</endtime>
  </times>
</clock>
测试代码

File file = new File("myFile.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Clock.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Clock clock = (Clock) jaxbUnmarshaller.unmarshal(file);

Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(clock, System.out);
输出


我的钟
2可在此处找到解决方案:

我可以将jaxb.properties放在Clock.java包中,也可以通过以下方式获取JABXContext实例:

JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {Clock.class}, null);

如果完全填充对象模型并封送它,会得到什么?另外,如果您在
jaxbContext
上调用
.getClass()
,您认为真正的类是什么?
类com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl
我终于找到了我的错误所在。未读取我的jaxb.properties,因为它不在我的域类的包中。现在它工作得很好!你能补充一下吗?这将帮助将来遇到这种情况的人找到你的问题。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<clock>
    <name>myClock</name>
</clock>
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {Clock.class}, null);