Reputation: 63
I want to unmarshal a given xml file using jaxb2. Here is the source xml document.
<?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>
for this I defined following 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>
Using JAXB I generated the classes for this but when I am unmarshalling I only able to get the Calendar Objects but not the nested "Exception" objects inside Calendar's ExceptionList. Following code will explain above
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());
}
}
}
Here is the logic for handler.unmarshal method
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;
}
It would be a great help if someone can explain how the object creation takes place while unmarshalling. Probably I am missing something small but important here.
Upvotes: 4
Views: 1702
Reputation: 6226
I think your schema is wrong, replace name="ExceptionCalendar"
by name="exception"
and regenerate JAXB objects.
<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>
Upvotes: 1