开发者

How to specify Object reference Datatype in JAXB

Is there an "XML Schema type to Java data type" binding for java.lang.Object in JAXB?

Simple Example

    <Channels>
      <EChannel>
        <Number>1</Number>
        <Format>0</Format>
        <VarNumber>11</VarNumber>
        <Name>First Channel</Name>
        <ChannelHandler></ChannelHandler>
      </EChannel>
      <EChannel>
        <Number>2</Number>
        <Format>0</Format>
        <VarNumber>22</VarNumber>
        <Name>Second Channel</Name>
        <ChannelHandler></ChannelHandler>
      </EChannel>
    </Channels>

Basically I'll first unmarshall the xml tree into an arraylist of Pojos (List)

And then in runtime I want to attach a java object to the ChannelHandler property.

I dont want to do anything manually inside the EChannel POJO, since I want these to be generated automatically usi开发者_如何学运维ng xjc

Thanks


Using @XmlAnyElement(lax=true) on a property of type Object should give you the behavior you are looking for.

When starting from an XML schema, a schema structure like the following will result in this annotation.

<xs:element name="ChannelHandler">
    <xs:complexType>
        <xs:sequence>
            <xs:any processContents="lax"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Full XML Schema

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="Channels">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="EChannel" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Number" type="xs:int"/>
                            <xs:element name="Format" type="xs:int"/>
                            <xs:element name="VarNumber" type="xs:int"/>
                            <xs:element name="Name" type="xs:string"/>
                            <xs:element name="ChannelHandler">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:any processContents="lax"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:any/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

For More Information

  • http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜