Unmarshalling complex xml with nested children

I want to unmount 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 the 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 classes for this, but when I'm unmarshalling, I can only get Calendar objects, but not nested Exception objects in the Calendar ExceptionList. The following code will be explained 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 of the 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 very helpful if someone can explain how the creation of an object occurs until it is disassembled. Probably I am missing something small but important here.

+4
source share
1 answer

I think your schema is wrong, replace name="ExceptionCalendar" with name="exception" and restore the 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> 
+1
source

Source: https://habr.com/ru/post/1499655/


All Articles