tags:

views:

281

answers:

2

I am writing a RESTful web service client. The service end point requires XML in this format:

<top-level-element type=\"array\">
    <element-key>
        <element>foo</element>
        <other-element>bar</element>
    </element-key>
</top-level-element>

I have Java code as follows:

public class Parent {
    @XmlElement(name="top-level-element")
    @XmlJavaTypeAdapter(TopLevelElementKeyAdapter.class)
    private HashMap<String, Integer> topLevelElement = new HashMap<String, Integer>();
}      

public final class TopLevelElementKeyAdapter extends
    XmlAdapter<MyElementMap, HashMap<String, Integer>>...

My code does everything I want, but I can't figure out how to get the 'type=\"array\" into my adapter. Thoughts?

A: 

You can add the following snippet to MyElementMap:

@XmlAttribute(name="type")
private final String type = "array";

This will magically show up as an attribute in your top-level-element XML tag!

Steve
A: 

Is there a way to add an attribute in another element except from the top-level one?

Leo