Java Mailing List Archive

http://www.junlu.com/

Home » user.castor »

[castor-user] The class for the root element 'array-list'

Zachariah Young

2010-02-17

Replies: Find Java Web Hosting

Author LoginPost Reply
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

©2008 junlu.com - Jax Systems, LLC, U.S.A.