prev table of contents next

2.2.16 Elements With Any Type

2.2.16.1 DOM Elements

The XML Schema language provides xsd:anyType which is the equivalent of java.lang.Object, i.e., it can be envisaged as the base type from which all simple and complex types are derived. To see how this is handled in JAXB, we define the complex type BagType which is nothing but a wrapper for any content.

<xsd:complexType name="BagType">
  <xsd:sequence>
    <xsd:element name="Content" type="xsd:anyType"/>
  </xsd:sequence>
</xsd:complexType>
The class that is generated by JAXB shouldn't come as a surprise:
public class BagType {
    protected Object content;

    public Object getContent() {
        return content;
    }

    public void setContent(Object value) {
        this.content = value;
    }
}
But what will be the class of the Content - if not java.lang.Object (which would be obtuse), then what? Well, the actual class doesn't really matter - the important thing is that it implements org.w3c.dom.Element, which is a subinterface of org.w3c.dom.Node. This means that, if you really have to, you can leave the cushy and plushy JAXB environment and continue with traditional DOM processing methods, e.g.:
Element content = (Element)bag.getContent();
String tag = content.getTagName();
Node<List> = content.getChildNodes();
// ...more DOM accesses...

If you need to marshal arbitrary content you'll have to create a content tree according to org.w3c.dom. Sun's JAXB currently uses the implementation from Apache's Xerces, i.e., class apache.xerces.internal.dom.ElementNSImpl.


prev table of contents next