XSD anytype and JAXB
I have an xsd definition (from www.tmforum.org ossj common api v1.5)
<element name="primaryKey" nillable="false">
<complexType mixed="false">
<complexContent mixed="false">
<extension base="anyType"/>
</complexContent>
开发者_JS百科 </complexType>
</element>
and would like to generate an xml as follows
<ossj-co-v1-5:primaryKey>mykey</ossj-co-v1-5:primaryKey>
The PrimaryKey class generated from the xsd using xjc requires a DOM Element to be stored in a list (see the generated PrimaryKey class at the bottom". "myKey" here is a TextNode and since its not an DOM Element, it cannot be added to xjc generated PrimaryKey class. How should I proceed to get the required output?
Here is the PrimaryKey class generated from the xsd
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"any"
})
public static class PrimaryKey {
@XmlAnyElement
protected List<Element> any;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
public List<Element> getAny() {
if (any == null) {
any = new ArrayList<Element>();
}
return this.any;
}
public Map<QName, String> getOtherAttributes() {
return otherAttributes;
}
}
The following object models would work for your scenario. I'll try to dig up the approprate schema customizations to produce these object models.
Option #1
You could have your code look like the following. This would mean that the element "primaryKey" would cause the object PrimaryKey to be instantiated with the corresponding text content being set on the any property.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"any" })
public static class PrimaryKey {
@XmlValue
protected String any;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
public List<Element> getAny() {
if (any == null) {
any = new ArrayList<Element>();
}
return this.any;
}
public Map<QName, String> getOtherAttributes() {
return otherAttributes;
}
}
Option #2
If you want an outer object to have a String property corresponding to the primaryKey you could do the following:
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
// @XmlElement is implied
private String primaryKey;
}
The Option#1 getAny() cannot return String as the signature returns List.
The Option#2 indeed works. Thanks!
Here is how my OSSJ code modification looks:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ManagedEntityKey", propOrder = {
"applicationContext",
"applicationDN",
"type",
"primaryKey"
})
public class ManagedEntityKey {
@XmlElement(required = true)
protected String primaryKey;
//protected ManagedEntityKey.PrimaryKey primaryKey;
And ofcourse the signature of the setters and getters should be modified.
精彩评论