Hi Uwe,

by default Castor calls entity.getList().add() for every element of the
list at unmarshaling. Does this match your observation.

You can change this default behaviour by specifing get-/set-methods at
field element. This is described at:
http://castor.codehaus.org/reference/html/XML%20data%20binding.html#xml.mapping.mappingfile.field

Regards
Ralf

Am 18.05.2011 14:56, schrieb Uwe:
>
>
> Hi,
>
> I have a problem with castors xml mapping.
>
>
> Starting point is a class with two private members: a list and a map.
>
> The setter for the list constructs the map from data
> from the list. The mapping file describes that only the list is
> mapped to xml.
>
> I construct an instance and use the setter for setting a
> list with three strings. This works fine.
>
> After serialization and deserialization the list with 3 strings
> is full available, but for the map only 1 instead of 3
> entries is built. The generated xml looks fine.
> Logging the setter I get that it is called with a list with
> one element !
>
> So: why is the setter only called with a list with one instance ?
> But: why is the list field ok after deserialisation ?
>
> I put the code and logging below this mail. I hope this is not
> to lengthy, I tried to reduce it as far as possible.
>
>
> Regards, Uwe
>
> +++ stdout with comments 
> ++++++++++++++++++++++++++++++++++++++++++++++++
>
>
> when constructing:
>
>     default cons called
>     setLi called with list of length 3
>     
>
> objects state after constructing. the left column
> comes from the list, the right column from the map
>
>     zero:  0
>     one:  1
>     two:  2
>
>
> generated xml is:
>     
>     <?xml version="1.0" encoding="UTF-8"?>
>     <reproduce-strange-castor-behaviour>
>         <li>zero</li>
>         <li>one</li>
>         <li>two</li>
>     </reproduce-strange-castor-behaviour>
>
>
> during deserialisation:
>
>     default cons called
>     setLi called with list of length 1
>
>
> objects state after deserialisation. the left column
> comes from the list, the right column from the map:
>
>     zero:  0
>     one:  null
>     two:  null
>
>
> +++ code +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> package sandbox;
>
> import java.io.StringReader;
> import java.io.StringWriter;
> import java.util.ArrayList;
> import java.util.HashMap;
> import java.util.List;
> import java.util.Map;
>
> import org.exolab.castor.mapping.Mapping;
> import org.exolab.castor.xml.Marshaller;
> import org.exolab.castor.xml.XMLContext;
>
> public class ReproduceStrangeCastorBehaviour {
>
>     private ArrayList<String> li;
>     private Map<String, Integer> ma;
>
>     public ReproduceStrangeCastorBehaviour() {
>         System.out.println("default cons called");
>     }
>
>     public List<String> getLi() {
>         return li;
>     }
>
>     public void setLi(ArrayList<String> li) {
>         System.out.println("setLi called with list of length
> "+li.size());
>         this.li = li;
>
>         // construct map
>         this.ma = new HashMap<String, Integer>();
>         for (int i = 0; i < li.size(); ++i)
>             this.ma.put(li.get(i), i);
>
>     }
>
>     public void print() {
>         for (String l : this.li) {
>             System.out.println(l + ":  " + this.ma.get(l));
>         }
>
>     }
>     
>     private static String toXML(Object o, XMLContext context) throws
> Exception {
>         Marshaller ms = context.createMarshaller();
>         StringWriter sw = new StringWriter();
>         ms.setWriter(sw);
>         ms.marshal(o);
>         return sw.toString();
>     }
>
>     private static Object fromXML(String xml, XMLContext context) throws
> Exception {
>         return context.createUnmarshaller().unmarshal(new
> StringReader(xml));
>     }
>
>     public static void main(String[] args) throws Exception {
>
>         // construct object rp
>         ReproduceStrangeCastorBehaviour rp = new
> ReproduceStrangeCastorBehaviour();
>
>         ArrayList<String> li = new ArrayList<String>();
>         li.add("zero");
>         li.add("one");
>         li.add("two");
>         rp.setLi(li);
>
>         // state of rp
>         rp.print();
>        
>         // serialisation + deserialisation
>         Mapping m = new Mapping();
>         m.loadMapping("castor_mappings/reproduce_strange_behavior.xml");
>         XMLContext context = new XMLContext();
>         context.addMapping(m);
>         String xml = toXML(rp, context);
>         System.out.println(xml);
>         rp = (ReproduceStrangeCastorBehaviour) fromXML(xml, context);
>
>         // state of rp
>         rp.print();
>     }
> }
>
> +++ mapping +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> <mapping>
>     <class name="sandbox.ReproduceStrangeCastorBehaviour">
>         <field name="li" type="string" collection="arraylist" />
>     </class>
> </mapping>
>
>


-- 

Syscon Ingenieurbüro für Meß- und Datentechnik GmbH
Ralf Joachim
Raiffeisenstraße 11
72127 Kusterdingen
Germany

Tel.   +49 7071 3690 52
Mobil: +49 173 9630135
Fax    +49 7071 3690 98

Internet: www.syscon.eu
E-Mail: [email protected]

Sitz der Gesellschaft: D-72127 Kusterdingen
Registereintrag: Amtsgericht Stuttgart, HRB 382295
Geschäftsleitung: Jens Joachim, Ralf Joachim


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to