Hey Brahma,

Unfortunately XML files can only have one root, and since Castor uses an
XML parser to read the XML files, you must use valid XML.  I have used
Castor for configuration purposes on two different projects.  In both
projects I needed to import multiple objects from one file.  For both of
them I used a "container" object that contains java.util.Sets for each
"root" class that I needed to unmarshal.  In your example, you would have
a Set of "class1" objects.

Your unmarshalling code retrieves the container object and then gets the
objects from the container.  (I usually have a getAllObjects() method in
my container object that then just compiles a large collection containing
the objects from all of the sets)  You can simply manually create a
"CastorContainer.java" file and map it to contain a set for each class of
objects you need to unmarshal or marshal.

This works fine if you only have a few different class types that need to
be unmarshalled.  However, on one project, I needed to make it easy to
add new class types into my XML file.  For that project, I created a
configuration file (you could use a properties or XML file) that I use to
create a container java source file at build time using the Jakarta
Velocity templating engine.  Since I generate my Castor mapping file
using XDoclet, Velocity also generates castor XDoclet tags in my
container source.  This setup allows me to simply define a new class in
my properties (actually I use XML) file, and when I do a new build,
Castor is setup to allow me to include the new class in my XML to be
unmarshalled.  Very easy (once you get it setup).

Under either approach, your example file would look something like:
<?xml version="1.0"?>
<container>

  <class1>
    <name>james</name>
    <id>34</id>
  </class1>
  <class1>
    <name>jack</name>
    <id>45</id>
  </class1>

</container>

where the container tag is mapped to my CastorContainer.java file.

I hope this gives you some ideas.  Alternatively you could put every
object in one file (but there are many cases where this approach isn't
acceptable).

Jeremy


On Tue, 10 Feb 2004 19:58:11 -0800, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> said:
> Hi Jeremy,
> 
> I saw your mail on the Castor mailing list. I am also
> facting the same prorblem. I have multiple objects in an xml
> file. I am not able to unmarshall this using the mapping
> file of the actual object.
> 
> Please let me know, what are the changes to made to the
> mapping file to get the unmarshalling done. This would be of
> great help to me. Also, pls let me know in case there is any
> other approach that you have used to accomplish this.
> 
> Thanks in advance.
> 
> Rgds,
> brahma
-- 
  Jeremy Haile
  [EMAIL PROTECTED]

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to