Hi daab wrote: > Thank you for the clarification. > > I am at the point where I want to marshall multiple objects into one XML > file. How do these objects relate to each other ? Are they part of e.g. an inheritance hierarchy ? Will each of those objects be mapped ?
> The root element must be unique, so is there a way to do this with > castor? > > I have found the following two posts helpful: > 1. https://docs.codehaus.org/display/CASTOR/MultipleObjectsTo1XML > "To marshall multiple objects into a single document, you can do the > following: > > marshaller m = new Marshaller(...); > m.setMarshalAsDocument(false); > > and then pass in multiple objects: > > m.marshal(obj1); > m.marshal(obj2); > > and so on.." > > > and: > > > 2. http://osdir.com/ml/java.castor.devel/2003-08/msg00321.html > > Keith Visco wrote: >> Maryanne, >> >> The "obvious" answer is probably your best bet, but you can also do the >> following: >> >> PrintWriter pw = new PrintWriter(writer, true); >> pw.println("<?xml version=\"1.0\"?>"); >> pw.println("<myroot>"); >> >> Marshaller m = new Marshaller(pw); >> m.setMarshalAsDocument(false); >> m.marshal(obj1); >> m.marshal(obj2); >> pw.println("</myroot>"); >> >> --Keith >> > > > Following the example on IBM's site closely, I now have: > package ibm.xml.castor; > > import java.io.*; > import org.exolab.castor.xml.Marshaller; > > public class MarshalTester { > > public static void main(String[] args) { > try { > BufferedWriter theBufferedFileWriter = new BufferedWriter(new > FileWriter("cds.xml")); > StringWriter theStringWriter = new StringWriter(); > > //1st CD object > CD cd1 = new CD("CD 1", "Artist 1"); > cd1.addTrack("Track 1"); > cd1.addTrack("Track 2"); > > //Marshall to our StringWriter > //Default is setMarshalAsDocument == true to set header > Marshaller m = new Marshaller(); > m.setWriter(theStringWriter); > m.marshal(cd1); > > //Prepare marshaller for appending more objects > m.setMarshalAsDocument(false); > > //2nd CD object > CD cd2 = new CD("myCd", "myArtist"); > cd2.addTrack("traack 1"); > cd2.addTrack("traack 2"); > > //Marshall 2nd CD to our StringWriter > m.marshal(cd2); > > //Write StringWriter buffer to file > theBufferedFileWriter.write(theStringWriter.toString()); > > theBufferedFileWriter.close(); > theStringWriter.close(); > } catch (Exception e) { > System.err.println(e.getMessage()); > e.printStackTrace(System.err); > } > } > } > CD class is Listing 2 on: > http://www.ibm.com/developerworks/xml/library/x-xjavacastor1/ > > So my concern is, if I simply set setMarshalAsDocument = false from the > beginning and set the header and root element manually, wouldn't that give > an error during unmarshalling because I'd need to change my class and > mapping to reflect the new root element? I can perhaps see why this isn't > implemented in castor; it would require the class to be changed upon > unmarshalling... and to what type of sequence? > > So I guess the only way is to specify another class that holds a sequence of > my base CD class and then marshall that. For big files, this might become a > memory problem though. Any suggestions on if I can avoid this problem? --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email

