prev table of contents next

2.2.12 Defining Types for XML Elements With Content

2.2.12.1 Content: A Value

The content of an XML element may be some value, or one or more subordinate elements, or even a combination of both. Let's look at an XML element with a value of some type. This is defined by a schema construct like this:

<xsd:element name="Quantity" type="xsd:int"/>
This does not define another type, but it may occur as some part of a complex type definition that describes the structure and attributes of the containing element, this element itself and its siblings. The xsd:element defines the XML tag, so that an example of this XML element is bound to look like this:
<Quantity>144</Quantity>
The Java code resulting from such an embedded element definition is part of some class definition, i.e., the one describing the containing element, and it consists of the declaration of an instance variable, a getter method (here: int getQuantity()) and, except for list types, a setter method (here: void setQuantity(int value)).

An element definition like this may also be provided for specifying the root element of an XML document. Obviously, such an element cannot be part of yet another type definition describing the structure of an enclosing element, simply because there is no such element. The code generated for a stand-alone element definition can be found in the class ObjectFactory which is generated along with all the classes derived from your schema's type definitions. So, from any stand-alone element definition that looks like this

<xsd:element name="Doc" type="DocType"/>
you may expect the generated class ObjectFactory to contain
public class ObjectFactory {
    private final static QName _Doc_QNAME = new QName("", "Doc");
    //...
    public JAXBElement<DocType> createDoc(DocType value) {
      return new JAXBElement<DocType>(_Doc_QNAME, DocType.class, null, value);
    }
    // ...
}
(We'll have a closer look at the other methods in this factory class in the section
The Object Factory.) Notice that you are not restricted to a single document root element.

The following subsections describe the structuring facilities of the XML Schema language for defining element content.


prev table of contents next