Polymorphism in XSD schema and JAXB classes

前端 未结 4 758
忘掉有多难
忘掉有多难 2021-01-31 10:01

I have an xml like this:


    
    
    

        
相关标签:
4条回答
  • 2021-01-31 10:09

    xsd:choice corresponds to the @XmlElements annotation. You could apply this annotation directly to your desired object model.

    For more information see:

    • http://bdoughan.blogspot.com/2010/10/jaxb-and-xsd-choice-xmlelements.html
    0 讨论(0)
  • 2021-01-31 10:12

    Use xs:extension in your schema and your JAXB classes will be inherited (extended) as you define in your schema.

    0 讨论(0)
  • 2021-01-31 10:20

    Maybe I'm not 'getting' the question, but what is wrong with..

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:complexType name="todo">
            <xs:sequence>
                <xs:choice maxOccurs="unbounded">
                    <xs:element name="doLaundry" type="task" />
                    <xs:element name="washCar" type="task" />
                    <xs:element name="tidyBedroom" type="task" />
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    
    
        <xs:complexType name="task">
            <xs:attribute name="cost" type="xs:int" />
            <xs:attribute name="experiencePoints" type="xs:int" />
        </xs:complexType>
    </xs:schema>
    
    0 讨论(0)
  • 2021-01-31 10:27

    I found the answer with the help of Blaise Doughan's article here: http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-xsitype.html

    This schema:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:complexType name="todo">
            <xs:sequence>
                <xs:choice maxOccurs="unbounded">
                    <xs:element name="doLaundry" type="doLaundry" />
                    <xs:element name="washCar" type="washCar" />
                    <xs:element name="tidyBedroom" type="tidyBedroom" />
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    
        <xs:complexType abstract="true" name="Task">
            <xs:attribute name="cost" type="xs:int" use="required" />
        </xs:complexType>
    
        <xs:complexType name="doLaundry">
            <xs:complexContent>
                <xs:extension base="Task">
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    
        <xs:complexType name="washCar">
            <xs:complexContent>
                <xs:extension base="Task">
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    
        <xs:complexType name="tidyBedroom">
            <xs:complexContent>
                <xs:extension base="Task">
                    <xs:attribute name="experiencePoints" type="xs:int" />
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    
    </xs:schema>
    

    combined with a binding file:

    <jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <jxb:bindings>
          <jxb:bindings schemaLocation="todo.xsd" node="/xs:schema/xs:complexType[@name='todo']/xs:sequence/xs:choice">
                <jxb:property name="Tasks"/>
            </jxb:bindings>
        </jxb:bindings>
    </jxb:bindings>
    

    Will give abstract and inherited classes as I described in the question. The binding file will change Jaxb's default method name from getDoLaundryOrWashCarOrTidyBedroom() to getTasks().

    0 讨论(0)
提交回复
热议问题