Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Relationships/Collections and Maps"

m (@XmlList and @XmlAttribute)
m (Replacing page with ''''Warning This page is obsolete. Please see ''[http://www.eclipse.org/eclipselink/documentation/2.4/ Developing JAXB Applications Using EclipseLi...')
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
'''[[Image:Elug_draft_icon.png|Warning]] This page is obsolete. Please see ''[http://www.eclipse.org/eclipselink/documentation/2.4/ Developing JAXB Applications Using EclipseLink MOXy]'' for current information.'''
|info=y
+
|toc=y
+
|eclipselink=y
+
|eclipselinktype=MOXy
+
|api=y
+
|apis= *
+
}}
+
=Collections =
+
JAXB contains several annotations to represent collections in XML:
+
* [[#.40XmlElement|@XmlElement]]
+
* [[#.40XmlElementWrapper|@XmlElementWrapper]]
+
* [[#.40XmlList|@XmlList]]
+
* [[#.40XmlList and @XmlAttribute|@XmlList and @XmlAttribute]]
+
* [[#.40XmlList and @XmlValue|@XmlList and @XmlValue]]
+
 
+
 
+
==Java Model==
+
To demonstrate the annotations, refer to the following model:
+
<source lang="Java">
+
 
+
import java.util.*;
+
import javax.xml.bind.annotation.*;
+
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Customer {
+
+
    private List<String> emailAddresses;
+
   
+
    public Customer() {
+
        emailAddresses = new ArrayList<String>();
+
    }
+
+
    public List<String> getEmailAddresses() {
+
        return emailAddresses;
+
    }
+
+
    public void setEmailAddresses(List<String> emailAddresses) {
+
        this.emailAddresses = emailAddresses;
+
    }
+
+
}
+
</source>
+
 
+
By applying different JAXB annotations, you can produce different XML.
+
 
+
 
+
==Demo Code==
+
This sample code converts the '''Customer''' object to XML.
+
 
+
<source lang="Java">
+
+
import javax.xml.bind.JAXBContext;
+
import javax.xml.bind.Marshaller;
+
+
public class Demo {
+
+
    public static void main(String[] args) throws Exception {
+
        JAXBContext jc = JAXBContext.newInstance(Customer.class);
+
+
        Customer customer = new Customer();
+
        customer.getEmailAddresses().add("janed@example.com");
+
        customer.getEmailAddresses().add("jdoe@example.org");
+
+
        Marshaller marshaller = jc.createMarshaller();
+
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+
        marshaller.marshal(customer, System.out);
+
    }
+
}
+
</source>
+
 
+
 
+
==Default==
+
By default, JAXB marshalls each item in the collection to an XML element:
+
 
+
<source lang="xml">
+
+
<customer>
+
    <emailAddresses>janed@example.com</emailAddresses>
+
    <emailAddresses>jdoe@example.org</emailAddresses>
+
</customer>
+
 
+
</source>
+
 
+
 
+
==@XmlElement==
+
Use the '''@XmlElement''' to control the name of the XML element that a collection item is marshalled to:
+
 
+
<source lang="java">
+
+
import java.util.*;
+
import javax.xml.bind.annotation.*;
+
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Customer {
+
+
    @XmlElement(name="email-address")
+
    private List<String> emailAddresses;
+
+
}
+
</source>
+
 
+
This will produce the following XML:
+
<source lang="xml">
+
+
<customer>
+
    <email-address>janed@example.com</email-address>
+
    <email-address>jdoe@example.org</email-address>
+
</customer>
+
</source>
+
 
+
 
+
== @XmlElementWrapper==
+
Use the '''@XmlElementWrapper''' annotation to add a grouping element to organize the collection:
+
 
+
<source lang="java">
+
import java.util.*;
+
import javax.xml.bind.annotation.*;
+
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Customer {
+
+
    @XmlElementWrapper(name="email-addresses")
+
    @XmlElement(name="email-address")
+
    private List<String> emailAddresses;
+
+
}
+
 
+
This will produce the following XML:
+
<source lang="xml">
+
<customer>
+
    <email-addresses>
+
        <email-address>janed@example.com</email-address>
+
        <email-address>jdoe@example.org</email-address>
+
    </email-addresses>
+
</customer>
+
</source>
+
 
+
 
+
==@XmlList==
+
Use the '''@XmlList''' annotation to represent the collection as space separated text. 
+
 
+
<source lang="Java">
+
import java.util.*;
+
import javax.xml.bind.annotation.*;
+
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Customer {
+
+
    @XmlList
+
    private List<String> emailAddresses;
+
+
}
+
</source>
+
 
+
This will produce the following XML:
+
<source lang="xml">
+
+
<customer>
+
    <emailAddresses>janed@example.com jdoe@example.org</emailAddresses>
+
</customer>
+
</source>
+
 
+
 
+
==@XmlList and @XmlAttribute==
+
Because [[#.40XmlList|@XmlList]] represents a collection in a single piece of text, it can be used with an XML attribute.
+
 
+
<source lang="Java">
+
+
import java.util.*;
+
import javax.xml.bind.annotation.*;
+
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Customer {
+
+
    @XmlList
+
    @XmlAttribute
+
    private List<String> emailAddresses;
+
+
}
+
</source>
+
 
+
This produces the following XML:
+
<source lang="xml">
+
+
<customer
+
    emailAddresses="janed@example.com jdoe@example.org"/>
+
</source>
+
 
+
==@XmlList and @XmlValue==
+
Because [[.40XmlList|XmlList]] represents a collection in a single piece of text, it is also compatible with a single text node.
+
 
+
<source lang="java">
+
+
import java.util.*;
+
import javax.xml.bind.annotation.*;
+
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Customer {
+
+
    @XmlList
+
    @XmlValue
+
    private List<String> emailAddresses;
+
+
}
+
</source>
+
 
+
This produces the following XML:
+
 
+
<source lang="xml">
+
<customer>janed@example.com jdoe@example.org</customer>
+
</source>
+
 
+
{{EclipseLink_MOXy
+
|previous= [[EclipseLink/UserGuide/MOXy/Simple_Values/Special_Schema_Types|Special Schema Types]]
+
|up=    [[EclipseLink/UserGuide/MOXy/Advanced_XML_Schema_Concepts|Advanced XML Schema Concepts]]
+
|next=      [[EclipseLink/UserGuide/MOXy/Relationships/Bidirectional_Relationships|Bidirectional Relationships]]
+
|version=2.2.0 Draft
+
}}
+

Latest revision as of 13:09, 30 January 2013

Warning This page is obsolete. Please see Developing JAXB Applications Using EclipseLink MOXy for current information.

Back to the top