我想使用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我为此生成了类,但是当我解组时,我只能获取Calendar对象而不是Calendar的ExceptionList中的嵌套“Exception”对象。 以下代码将在上面解释
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;
}
如果有人可以解释在解组时如何进行对象创建,那将是一个很大的帮助。可能我错过了一些小而重要的东西。
答案 0 :(得分:1)
我认为您的架构错误,请将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>