Difference between revisions of "EclipseLink/UserGuide/MOXy/Type Level/Handling Inheritance"

From Eclipsepedia

Jump to: navigation, search
m (New page: = Handling Inheritance = == Using xsi:type Attribute == You can use the '''xsi:type''' attribute to represent inheritance in JAXB. In this example an abstract super class ('''ContactIn...)
 
m (Using xsi:type Attribute)
Line 57: Line 57:
 
}
 
}
 
</source>
 
</source>
 +
 +
 +
 +
In this example, the '''xsi:type''' attribute represents inheritance.
 +
 +
<source lang="java">
 +
package blog.inheritance;
 +
 +
import javax.xml.bind.JAXBContext;
 +
import javax.xml.bind.Marshaller;
 +
 +
public class Demo {
 +
 +
    public static void main(String[] args) throws Exception {
 +
        Customer customer = new Customer();
 +
        Address address = new Address();
 +
        address.setStreet("1 A Street");
 +
        customer.setContactInfo(address);
 +
 +
        JAXBContext jc = JAXBContext.newInstance(Customer.class, Address.class, PhoneNumber.class);
 +
 +
        Marshaller marshaller = jc.createMarshaller();
 +
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 +
        marshaller.marshal(customer, System.out);
 +
    }
 +
 +
}
 +
</source>
 +
 +
The above sample code produces the following XML.
 +
<source lang="xml">
 +
 +
<customer>
 +
    <contactInfo
 +
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 +
    xsi:type="address">
 +
        <street>1 A Street</street>
 +
    </contactInfo>
 +
</customer>
 +
</source>
 +
Note the '''xsi:type''' attribute on the '''contactInfo''' element.

Revision as of 10:30, 24 March 2011

Handling Inheritance

Using xsi:type Attribute

You can use the xsi:type attribute to represent inheritance in JAXB.

In this example an abstract super class ('ContactInfo) contains all types of contact information. Address and PhoneNumber are the concrete implementations of ContactInfo.

package blog.inheritance;
 
public abstract class ContactInfo {
 
}
 
public class Address extends ContactInfo {
 
    private String street;
 
    public String getStreet() {
        return street;
    }
 
    public void setStreet(String street) {
        this.street = street;
    }
 
}
 
public class PhoneNumber extends ContactInfo {
 
}


Because the Customer object can have different types of contact information, its property refers to the superclass.

 
package blog.inheritance;
 
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class Customer {
 
    private ContactInfo contactInfo;
 
    public ContactInfo getContactInfo() {
        return contactInfo;
    }
 
    public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }
 
}


In this example, the xsi:type attribute represents inheritance.

 
package blog.inheritance;
 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
 
public class Demo {
 
    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        Address address = new Address();
        address.setStreet("1 A Street");
        customer.setContactInfo(address);
 
        JAXBContext jc = JAXBContext.newInstance(Customer.class, Address.class, PhoneNumber.class);
 
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }
 
}

The above sample code produces the following XML.

 
<customer>
    <contactInfo 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:type="address">
        <street>1 A Street</street>
    </contactInfo>
</customer>

Note the xsi:type attribute on the contactInfo element.