JAXB 2.0 supports the following annotations for defining open content. (Open content allows content not statically defined in XML schema to occur in an XML instance):
The @XmlAnyElement annotation maps to xs:any, which binds to the .NET type System.Xml.XmlElement[].
The @XmlAnyAttribute annotation maps to xs:anyAttribute, which binds to the .NET type System.Xml.XmlAttribute[].
Example: Using @XmlAnyElement for open content
//-- Java code fragment
@XmlType(propOrder={"name", "age", "oc"})
public class OcPerson {
@XmlElement(required=true)
public String name;
public int age;
// Define open content
@XmlAnyElement
public List<Object> oc;
}
//-- Schema fragment
<xs:complexType name="ocPerson">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:int"/>
<xs:any minOccurs="0" maxOccurs="unbounded">
</xs:sequence>
</xs:complexType>
// .NET auto generated code from schema
public class ocPerson
{
private String name;
private int age;
private System.Xml.XmlElement[] anyField;<
public String name { ... }
public int age { ... }
public System.Xml.XmlElement[] Any {
{
get { return this.anyField; }
set { this.anyField = value; }
}
}
Example: Using @XmlAnyAttribute for open content
//-- Java code fragment
@XmlType(propOrder={"name", "age"}
public class OcPerson {
public String name;
public int age;
// Define open content
@XmlAnyAttribute
public java.util.Map oc;
}
//-- Schema fragment
<xs:complexType name="ocPerson">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:int"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
// .NET auto generated code from schema
public class ocPerson
{
private String name;
private double age;
private System.Xml.XmlAttribute[] anyAttrField;<
public String name { ... }
public double age { ... }
public System.Xml.XmlElement[] anyAttr {
{
get { return this.anyAttrField; }
set { this.anyAttrField = value; }
}
}