users@jaxb.java.net

XMLAdapter unmarshal sequence to Map

From: Smith, Matthew (BRS) <"Smith,>
Date: Thu, 8 Jan 2009 17:02:29 -0500

Ok, I'm stumped.

I want my sequence to be umarshaled as a Map so I'm trying to create an
XMLAdaptor so that I can unmarshal a sequence of tags into a Map.

E.g. unmarshal some XML:

----------------------------------
<config>
   <field id="1">...</field>
   <field id="2">...</field>
   <field id="3">...</field>
   <field id="4">...</field>
</config>
----------------------------------

into a class Config:

----------------------------------
@XmlRootElement(name = "config")
public class Config {
    @XmlJavaTypeAdapter(FieldListAdapter.class)
    @XmlElement(name = "field")
    protected Map<String,Field> fields;
 
    /* accessors omitted for brevity */
}
----------------------------------

With this adapter:

----------------------------------
public class FieldListAdapter extends XmlAdapter<Field[], Map<String,
Field>> {
    public Map<String, Field> unmarshal(Field[] value) {
        Map<String, Field> r = new HashMap<String, Field>();
        for (Field c : value)
            r.put(c.getId(), c);
        return r;
    }

    public Field[] marshal(Map<String, Field> value) {
        return value.values().toArray(new Field[value.size()]);
    }
}
----------------------------------

So my adapter unmarshal method gets called but 'value' is empty. Why?
If I change my Config class to use a List instead of a Map and take away
the adapter the data gets unmarshaled so I know it's not my XML.
I even tried creating an adapter to convert Field[] to List<Field> but
value is still empty.
Cleary JAXB can do it on it's own, so why can't I? Am I just doing
something wrong or is what I want impossible?

Thanks for your help.

-Matthew


THE INFORMATION CONTAINED IN THIS MESSAGE AND ANY ATTACHMENT MAY BE
PRIVILEGED, CONFIDENTIAL, PROPRIETARY OR OTHERWISE PROTECTED FROM
DISCLOSURE. If the reader of this message is not the intended recipient,
you are hereby notified that any dissemination, distribution, copying or
use of this message and any attachment is strictly prohibited. If you
have received this message in error, please notify us immediately by
replying to the message and permanently delete it from your computer and
destroy any printout thereof.



THE INFORMATION CONTAINED IN THIS MESSAGE AND ANY ATTACHMENT MAY BE PRIVILEGED, CONFIDENTIAL, PROPRIETARY OR OTHERWISE PROTECTED FROM DISCLOSURE. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution, copying or use of this message and any attachment is strictly prohibited. If you have received this message in error, please notify us immediately by replying to the message and permanently delete it from your computer and destroy any printout thereof.