In JAXB, is it possible to define the following case:
<parameters>
<parameter name="param1">value1</param>
<parameter name="someCollection">
<parameters>
<parameter name="param2">value2</param>
<parameter n开发者_StackOverflow中文版ame="param3">value3</param>
</parameters>
</parameter>
</parameters>
where a parameter
element can sometimes have a simple value ("value1") and sometimes it can have other elements (such as another parameters
element).
Thanks!
You can use the @XmlPath
extension in EclpseLink JAXB (MOXy), I'm the MOXy text lead.
- http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html
The @XmlPath
extension will enable you to map the following class:
package blog.predicate;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name="node")
@XmlType(propOrder={"firstName", "lastName", "address", "phoneNumbers"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlPath("node[@name='first-name']/text()")
private String firstName;
@XmlPath("node[@name='last-name']/text()")
private String lastName;
@XmlPath("node[@name='address']")
private Address address;
@XmlPath("node[@name='phone-number']")
private List<PhoneNumber> phoneNumbers;
}
To an XML document that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<node>
<node name="first-name">Jane</node>
<node name="last-name">Doe</node>
<node name="address">
<node name="street">123 A Street</node>
</node>
<node name="phone-number" type="work">555-1111</node>
<node name="phone-number" type="cell">555-2222</node>
</node>
精彩评论