我想使用 jaxb2 解码给定的 xml 文件。 这是源 xml 文档。
<?xml version="1.0" encoding="UTF-8"?>
<root>
<calendarList>
<calendar>
<calendarCode>Default</calendarCode>
<weeklyDefault>1111111</weeklyDefault>
<exceptionList>
<exception>
<exceptionDate>2012-03-01T00:00:00</exceptionDate>
<isOpen>false</isOpen>
</exception>
<exception>
<exceptionDate>2012-03-02T00:00:00</exceptionDate>
<isOpen>false</isOpen>
</exception>
</exceptionList>
</calendar>
<calendar/>
<calendarList>
</root>
为此我定义了以下 xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="2.0">
<xsd:element name="root" type="Root" />
<xsd:complexType name="Root">
<xsd:sequence>
<xsd:element name="calendarList" type="CalendarList" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="CalendarList">
<xsd:sequence>
<xsd:element name="calendar" type="Calendar" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Calendar">
<xsd:sequence>
<xsd:element name="calendarCode" type="xsd:string" />
<xsd:element name="weeklyDefault" type="xsd:string" />
<xsd:element name="exceptionList" type="ExceptionList" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ExceptionList">
<xsd:sequence>
<xsd:element name="exceptionCalendar" type="ExceptionCalendar" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ExceptionCalendar">
<xsd:sequence>
<xsd:element name="exceptionDate" type="xsd:dateTime" />
<xsd:element name="isOpen" type="xsd:boolean"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
我使用 JAXB 为此生成了类,但是当我解码时,我只能获取日历对象,但不能获取日历异常列表中嵌套的“异常”对象。 下面的代码会解释上面的内容
public void CheckResults(filePath){
Root ods = handler.unmarshal(filePath);
for(Calendar calendar : ods.getCalendarList().getCalendar())
{
System.out.println(calendar.getCalendaeCode()); //Here I have the element calendar
//but calendar.getExceptionList().getExceptionCalendar() has no member
for (ExceptionCalendar expCal : calendar.getExceptionList().getExceptionCalendar())
{
System.out.println(expCal.getExceptionDate());
}
}
}
这是 handler.unmarshal 方法的逻辑
public Root unmarshal(String filePath) {
try{
JAXBContext jc = JAXBContext.newInstance(DOMAIN_PKG);
Unmarshaller unmarsaller = jc.createUnmarshaller();
JAXBElement<Root> oDS;
if(filePath.isEmpty()) {
oDS = (JAXBElement<Root>) unmarsaller.unmarshal(System.in);
} else {
File file = new File(filePath);
oDS = (JAXBElement<Root>) unmarsaller.unmarshal(file);
}
return oDS.getValue();
}catch(JAXBException exp){
exp.printStackTrace();
}
return null;
}
如果有人能解释在解码时对象创建是如何发生的,那将是一个很大的帮助。可能我在这里遗漏了一些小而重要的东西。
最佳答案
我认为您的架构有误,将 name="ExceptionCalendar"
替换为 name="exception"
并重新生成 JAXB 对象。
<xsd:complexType name="ExceptionList">
<xsd:sequence>
<xsd:element name="exception" type="ExceptionCalendar" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ExceptionCalendar">
<xsd:sequence>
<xsd:element name="exceptionDate" type="xsd:dateTime" />
<xsd:element name="isOpen" type="xsd:boolean"/>
</xsd:sequence>
</xsd:complexType>
关于java - 解码具有嵌套子元素的复杂 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18515676/