How do I add an attribute to a HashMap using JAXB?
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")
@Xm开发者_Python百科lJavaTypeAdapter(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?
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!
精彩评论