Hey, it is great to also learn from Castor users, because I didn't know about that feature until now ;-)
Spring 3 O/X provides a rough code abstraction to keep O/X tools interchangeable. Of course at the cost of distinctive features, such as the list-at-root-feature. From my point of view, I'd either suggest to abandon Spring's CastorMarshaller and write your own factory bean, or if you depend on Spring's Marshaller/Unmarshaller interfaces, you could extend CastorMarshaller and modify it to use your list-at-root Unmarshaller, which I think could get really messy because Spring uses XmlContext internally to bootstrap Castor and the only way to use the list-at-root-feature is to call the Castor Unmarshaller constructor yourself. Hope that helps. Regards, Lukas Am 18.02.2010 um 16:24 schrieb Zachariah Young: > Lukas, > > Thanks for your quick reply. I understand what you are say. At this point > I'm trying to avoid creating the parent class. > > So I went back and created following example > http://www.castor.org/how-to-map-a-list-at-root.html. This work prefectly. > > But I'm now trying to wire all this up in Spring 3. > > Working sample > > Mapping mapping1 = new Mapping(); > mapping1.loadMapping("mapping1.xml"); > FileReader reader1 = new FileReader("order.xml"); > > Unmarshaller unmarshaller = new Unmarshaller(ArrayList.class); >-- I think > this is the piece I'm missing. > unmarshaller.setMapping(mapping1); > ArrayList<OrderItem> orders = (ArrayList<OrderItem>) > unmarshaller.unmarshal(reader1); > > Spring sample > castorMarshaller is what I'm injecting from my IOC > ArrayList<OrderItem> doc = (ArrayList<OrderItem>) > this.castorMarshaller.unmarshal(new StreamSource(is)); > > <bean id="castorMarshaller" > class="org.springframework.oxm.castor.CastorMarshaller"> > <property name="mappingLocation" value="classpath:mapping.xml" /> > </bean> > Question is how do I set ArrayList for the unmarshal to work? > > Thanks for the help > > > On Thu, Feb 18, 2010 at 12:04 AM, Lukas Lang <[email protected]> wrote: > Hey Zachariah, > > there is nothing wrong with the way you are using Castor. The problem in your > case is that the XML specification states the following: > > "There is exactly one element, called the root, or document element, no part > of which appears in the content of any other element." [1] > > This contract is violated by your (generated) XML instance. > > The reason why Castor can deal with your Collection and produce valid XML > output is simply because Castor uses a lot of introspection. It is more > complicated the other way round, because the Unmarshaller on the one hand has > no idea how to handle a non well formed XML instance and on the other hand > does not know what type the client code is expecting as there don't exist any > generic methods in the Castor XML API. > > So, back to your problem: > > Introduce a parent element and embed your Collection. > > <mapping> > <class name="Farm" type="com.zandroid.model.Farm"> > <field name="pets" type=""com.zandroid.model.Pet" > collection="arraylist"> > <bind-xml name="pet"/> > </field> > ... > > Your Farm domain object simply holds a List of Pet elements. Remember to add > accessors (getX/setX)! > > If you need further information, please consider the reference guide[2]. > > Regards, > Lukas > > [1] http://www.w3.org/TR/xml/#dt-root > [2] http://castor.codehaus.org/reference/html-single/index.html > > > Am 18.02.2010 um 05:09 schrieb Zachariah Young: > > > I'm trying to work through a simple use case for Castor and I'm getting the > > following error. What I'm trying to do is read in an XML file with a list > > of objects. > > > > Exception in thread "main" org.exolab.castor.xml.MarshalException: The > > class for the root element 'array-list' could not be found.{File: [not > > available]; line: 2; column: 13} > > > > My simple entity looks like the following. > > > > public class Pet implements Serializable { > > > > private String Name; > > private String Type; > > > > public void setName(String name) { > > Name = name; > > } > > > > public String getName() { > > return Name; > > } > > > > public void setType(String type) { > > this.Type = type; > > } > > > > public String getType() { > > return Type; > > } > > } > > My mapping file looks like this. > > > > <mapping> > > <class name="com.zandroid.model.Pet" auto-complete="true"> > > <map-to xml="pet"/> > > <field name="name" type="string"> > > <bind-xml name="name" node="element"/> > > </field> > > <field name="type" type="string"> > > <bind-xml name="type" node="element"/> > > </field> > > </class> > > </mapping> > > > > In my main class I have the follow code. > > > > public static void main(String[] args) throws IOException, > > MappingException, MarshalException, ValidationException { > > > > List<Pet> arrayList = new ArrayList<Pet>(); > > Pet pet = new Pet(); > > pet.setName("Zoe"); > > pet.setType("Dog"); > > arrayList.add(pet); > > > > Pet pet1 = new Pet(); > > pet1.setName("Gary"); > > pet1.setType("Cat"); > > arrayList.add(pet1); > > > > Writer fileWriter = new FileWriter("text.xml"); > > > > Mapping mapping = new Mapping(); > > mapping.loadMapping("mapping.xml"); > > > > XMLContext context = new XMLContext(); > > context.addMapping(mapping); > > > > Marshaller marshaller = context.createMarshaller(); > > marshaller.setWriter(fileWriter); > > > > marshaller.marshal(arrayList); > > > > context.createUnmarshaller().setClass(ArrayList.class); > > > > FileReader reader = new FileReader("text.xml"); > > > > ArrayList<Pet> pets = > > (ArrayList<Pet>)context.createUnmarshaller().unmarshal(reader); > > > > for (Pet pet2 : pets) { > > System.out.println(pet2.getName()); > > } > > } > > > > This code creates the following xml file. > > <array-list> > > <pet><name>Zoe</name><type>Dog</type></pet> > > <pet><name>Gary</name><type>Cat</type></pet> > > </array-list> > > > > So when I go Unmarshaller the XML file I get the The class for the root > > element 'array-list'. > > > > 1. Should I be doing something different in my mapping file? > > 2. Should I create another class that has a property of List<Pet>? > > 3. Is this the correct use case for Castor? > > > > Note: I'm also trying to get this to work in Spring 3. Also I have review > > this article http://www.castor.org/how-to-map-a-list-at-root.html > > > > Thanks for the help. > > -- > > Zachariah Young > > http://zachariahyoung.com > > > > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > > > > -- > Zachariah Young > President of NW Arkansas .Net User Group > Co-Founder of Virtual ALT.NET > INETA VUG Mentor > http://zachariahyoung.com > [email protected] > (479) 966-9169

