tags:

views:

59

answers:

1

Hello,

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>
   </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;
    }

}
A: 

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;

}
Blaise Doughan