Andreas,

You are getting more than one root element because you are calling the marshal method multiple times (once for each item in the ArrayList). Try changing your code as such:

  Mapping mapping = new Mapping();
  mapping.loadMapping("castor-xml-mapping.xml");
  Marshaller marshaller = new Marshaller(writer);
  marshaller.setMapping(mapping);
  marshaller.marshal(al);

If you want to change the name of the root element you can do so as such:

  Marshaller marshaller = new Marshaller(writer);
  marshaller.setMapping(mapping);
  marshaller.setRootElement("root");
  marshaller.marshal(al);

--Keith

Andreas Vombach wrote:

I've been working successfully with castor JDO for a while now. I was asked to export the data from our db which tables have JDO representations to xml/text file. So I get all rows with a query and store them in an ArrayList like:


           OQLQuery query = db.getOQLQuery("SELECT a FROM account a");
           QueryResults results = query.execute();
           while (results.hasMore())
           {
               account acc = (account) results.next();
               al.add(acc);
           }

Now I take an XML mapping and marshal the ArrayList elements:

Mapping mapping = new Mapping();
mapping.loadMapping("castor-xml-mapping.xml");
Marshaller marshaller = new Marshaller(new OutputStreamWriter(System.out));
marshaller.setMapping(mapping);
Iterator itr = al.iterator();
while (itr.hasNext())
{
account acc = (account) itr.next();
marshaller.marshal(acc);
}


Looks fine but the problem is that there is no XML root element. I would like to transform an XSL transformation afterwards so xalan will complain that there is more tan one root node. Another question in this context is if it would be possible to marshal into a DOM tree or something similar dirextly rather than fetching the marshalled stream and reparsing it for transforming. A marshaller can be constructed with a |org.xml.sax.DocumentHandler or ||org.w3c.dom.Node, this may lead me to a solution if I (probably) have only one root element.|
||
|Thanks for any ideas/hints|
|Andreas|
||




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






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

Reply via email to