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

