Thanks Wolfgang. This was a big help.
-Matthew
________________________________
From: Wolfgang Laun [mailto:wolfgang.laun_at_gmail.com]
Sent: Friday, January 09, 2009 5:42 AM
To: users_at_jaxb.dev.java.net
Subject: Re: XMLAdapter unmarshal sequence to Map
This is the closest to your setup that I know how to handle with
JAXB annotations. Notice that the adapter is now set up between
Fields (new) and Config. Wrapper is just there so that the adapted
element isn't the root element.
-W
@XmlRootElement(name = "wrapper")
public class Wrapper {
public Config config;
}
//=============================================
@XmlJavaTypeAdapter(FieldListAdapter.class)
public class Config {
public Map<String,Field> field = new HashMap<String,Field>();
}
//=============================================
public class FieldListAdapter extends XmlAdapter<Fields,Config> {
@Override
public Config unmarshal( Fields value ) {
Config c = new Config();
for( Field f : value.carray ) c.field.put( f.getId(), f );
return c;
}
@Override
public Fields marshal( Config bound ){
Fields fields = new Fields();
Collection<Field> c = bound.field.values();
fields.carray = c.toArray(new Field[c.size()]);
return fields;
}
}
//=============================================
public class Fields {
@XmlElement(name="field")
public Field[] carray;
}
//=============================================
<wrapper>
<config>
<field id="1"></field>
<field id="2"></field>
<field id="3"></field>
<field id="4"></field>
</config>
</wrapper>
On Thu, Jan 8, 2009 at 11:02 PM, Smith, Matthew (BRS)
<Matthew.Smith_at_blackrock.com> wrote:
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.
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.