4

我想使用 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 我为此生成了类,但是当我解组时,我只能获取日历对象,但不能获取日历的 ExceptionList 中嵌套的“异常”对象。以下代码将在上面解释

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;
    }

如果有人可以解释在解组时如何创建对象,那将是一个很大的帮助。可能我在这里遗漏了一些小而重要的东西。

4

1 回答 1

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>
于 2013-08-29T16:33:49.910 回答