Hi Reinhard,

have a look at the following mapping that allows me to marshal the given
Java object instances to your desired XML output structure.

<mapping>
   <class name="xml.reinhard.A">
      <description>Mapping for Class com.xyz.A</description>
      <map-to xml="A" ns-uri="http://xyz.com/A"; ns-prefix="a" />
      <field name="strings" collection="arraylist" type="string">
         <bind-xml name="string" node="element" location="b"/>
      </field>
   </class>
</mapping>

Please note that I changed the package when running the test(s) locally.

Regards
Werner

Reinhard Weiss wrote:
> hi,
> 
> i'am using castor-xml-1.0.5 but even with 1.2 the problem remains:
> i cannot completely unmarshal an object from the xml i have marshalled before 
> using a mapping file.
> 
> i have a class as simple as this:
> 
> 
> 
> package com.xyz;
> import java.util.List;
> import java.util.ArrayList;
> public class A
> {
>   private List<String> strings;
>   public A()
>   {
>     this.strings = new ArrayList<String>(3);
>     this.strings.add("one");
>     this.strings.add("two");
>     this.strings.add("three");
>   }
>   public void setStrings(List<String> strings)
>   {
>     this.strings = strings;
>   }
>   public List<String> getStrings()
>   {
>     return this.strings;
>   }
> }
> 
> 
> 
> the mapping file looks as follows
> 
> 
> 
> <mapping>
>   <class name="com.xyz.A">
>     <description>Mapping for Class com.xyz.A</description>
>     <map-to xml="A" ns-uri="http://xyz.com/A"; ns-prefix="a" />
>     <field name="strings" collection="arraylist" type="string" 
> container="false">
>       <bind-xml name="b" node="element"/>
>     </field>
>   </class>
> </mapping>
> 
> 
> 
> it marshals:
> 
> 
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <a:A xmlns:a="http://xyz.com/A";>
>   <!-- notice that there is no namespace anymore ??? -->
>   <b> 
>     <string>one</string>
>     <string>two</string>
>     <string>three</string>
>   </b>
> </a:A>
> 
> 
> 
> ok, the structure of the xml is finally what i want. 
> i use the 'container="false"' attribute in the 'field' element to get one 
> 'string' element per list item (as suggested on the how-to page). otherwise 
> all strings were merged into a single 'string' element containing 
> "onetwothree" (however - all elements with correct namespaces). but for some 
> reason castor suppresses the namespace when i use the container attribute.
> 
> then, if i ignore the namespace issue and try to unmarshal the xml, i don't 
> get back my list of strings any more. as castor failed to handle the 'b' 
> element throwing a MarshalException ('unable to find FieldDescriptor for 'b') 
> first, i have set the 'ignoreExtraElements' property of the unmarshaller to 
> 'true' (using a 'location' attribute with 'b/string' in the 'bind-xml' 
> element of my mapping instead didn't help as well).
> 
> 
> 
> my test looks as follows
> 
> 
>   public void testMarshalUnmarshal()
>   throws Exception
>   {
>     A a = new A();
>     
>     Mapping mapping = new Mapping();
>     mapping.loadMapping("a-mapping.xml");
>     
>     StringWriter writer = new StringWriter();
>     
>     // global marshaller
>     marshaller.setWriter(writer);
>     marshaller.setMapping(mapping);
>     marshaller.marshal(a);
>     
>     String marshalled = writer.toString();
>     if (logger.isDebugEnabled) logger.debug(marshalled);
> 
>     // global unmarshaller
>     unmarshaller.setMapping(mapping);
>     unmarshaller.setIgnoreExtraAttributes(true);
>     unmarshaller.setIgnoreExtraElements(true);
>     
>     StringReader reader = new StringReader(marshalled);
>     
>     Object unmarshalled = unmarshaller.unmarshal(reader);
>     
>     assertNotNull(unmarshalled);
>     assertTrue(unmarshalled instanceof A);
> 
>     // this assertion fails
>     assertNotNull(((A) unmarshalled).getStrings());
>     assertEquals(a.getStrings().size(), ((A) 
> unmarshalled).getStrings().size());
>   }
> 
> 
> 
> in the end i am still looking for a proper mapping.xml to get the following 
> xml:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <a:A xmlns:a="http://xyz.com/A";>
>   <a:b> 
>     <a:string>one</a:string>
>     <a:string>two</a:string>
>     <a:string>three</a:string>
>   </a:b>
> </a:A>
> 
> which i can successfully unmarshal again.
> 
> any kind of coaching would be nice!
> thx in advance,
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> DI Reinhard Weiss
> Java Developer
> 
> ANECON Software Design und Beratung G.m.b.H.
> Alser Straße 4 / Hof 1
> A-1090 WIEN
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Zusätzliche Pflichtangaben von Kapitalgesellschaften gem. § 14 UGB:
> FN166941b | Handelsgericht Wien | Firmensitz Wien
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
> 
>     http://xircles.codehaus.org/manage_email
> 
> 

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

    http://xircles.codehaus.org/manage_email


Reply via email to