Search This Blog

Thursday, 16 May 2013

More on Complex Types

In the last post we were able to map complex xml elements to classes. However it is recommended to build complex types and associate them to elements.
We saw a similar thing in the example of simple elements.
  1.  Define a top level type.
  2. Define elements of the specific type.
I modified the previous xsd accordingly:
<element name="attributedAddress2" type="si:attributedAddressType" />

<complexType name="attributedAddressType">
 <attribute name="city" type="string" use="optional" default="-" />
 <attribute name="zipcode" type="integer" use="required" />
 <attribute name="country" type="string" fixed="India" />
The code generated by XJC in this case is as below:
@XmlType(name = "attributedAddressType")
public class AttributedAddressType {

 protected String city;
 @XmlAttribute(required = true)
 protected BigInteger zipcode;
 protected String country;
 // same code as the previous example - setter getters

Here JAXB generated a class for the complex type. Hence we do not see the XmlRootElement annotation here.
For marshaling we need to create elements of type attributedAddress2. This is shown in the code for ObjectFactory:
private final static QName _AttributedAddress2_QNAME = new 
        QName("", "attributedAddress2");

@XmlElementDecl(namespace = "", name = "attributedAddress2")
public JAXBElement<AttributedAddressType> createAttributedAddress2(
 AttributedAddressType value) {
 return new JAXBElement<AttributedAddressType>(
  _AttributedAddress2_QNAME, AttributedAddressType.class, null,

public AttributedAddressType createAttributedAddressType() {
        return new AttributedAddressType();
To get the xml representation :
static void testMarshal() throws JAXBException {
 final ObjectFactory objectFactory = new ObjectFactory();
 AttributedAddressType addressType = objectFactory

 JAXBElement<AttributedAddressType> address2 = objectFactory
 final JAXBContext jContext = JAXBContext

 final Marshaller marshaller = jContext.createMarshaller();
 final StringWriter stringWriter = new StringWriter();
 marshaller.marshal(address2, stringWriter);

And the output is :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<attributedAddress2 zipcode="411020" city="Pune"
 xmlns="" />
To convert back this XML to java:
static void testUnmarshal() throws JAXBException {
 final JAXBContext jContext = JAXBContext
 final Unmarshaller unmarshaller = jContext.createUnmarshaller();
 final StreamSource source = new StreamSource(
 AttributedAddressType address1 = unmarshaller.unmarshal(source,
 System.out.println(address1.getCity() + ", " + address1.getCountry()
  + " - " + address1.getZipcode());
Some might find this absence of class an irritant. Working with JAXBElement is not the only way here. For this we need to define the xsd as below:
<element name="addressInstance">
   <element name="address" type="si:attributedAddressType" />
The corresponding class generated by XJC is :
@XmlType(name = "", propOrder = {"address"})
@XmlRootElement(name = "addressInstance")
public class AddressInstance {

 @XmlElement(required = true)
 protected AttributedAddressType address;

 // setters and getters


1 comment: