Hi Dennis: Thanks for the prompt & helpful reply. The only gotcha I found was making sure the individual marshallings got written to the underlying stream: so I got a reference to the XML writer and flush()'d it. Result:
IXMLWriter wrapperWriter = null; for ( int i = 0; i < objects.size(); i++ ) { Object object = objects.get( i ); IBindingFactory bFact = BindingDirectory.getFactory( object.getClass() ); IMarshallingContext mCtx = bFact.createMarshallingContext(); mCtx.setIndent( 4 ); mCtx.setOutput( out, "UTF-8" ); IXMLWriter writer = mCtx.getXmlWriter(); // set up document if ( i == 0 ) { mCtx.startDocument( "UTF-8", null ); writer.startTagClosed( 0, "wrapperElement" ); wrapperWriter = writer; } // marshall object ((IMarshallable)object).marshal( mCtx ); writer.flush(); // clean up after last object if ( i == objects.size() - 1 ) { wrapperWriter.endTag( 0, "wrapperElement" ); wrapperWriter.flush(); } } Regards, Richard Rodgers Quoting Dennis Sosnoski <[EMAIL PROTECTED]>: > Hi Richard, > > You don't need to know the index of the object class to make your code > work, you can just cast the instance of the class to IMarshallable and > call the marshal() method on the object instance. This interface is > added by JiBX as part of the binding compile. > > Aside from that, in order for the result to be a valid XML document > you're going to need to add a wrapper element around all these > individual roots. Writing text directly to the output stream is > generally not a great way of doing things - another alternative would be > to just add writing the XML declaration and the wrapper element start > tag inside your loop (after you've gotten the marshalling context, and > only when i == 0), then finish with the wrapper element end tag when > after writing the root at i == roots.length-1. > > - Dennis > > Dennis M. Sosnoski > SOA, Web Services, and XML > Training and Consulting > http://www.sosnoski.com - http://www.sosnoski.co.nz > Seattle, WA +1-425-296-6194 - Wellington, NZ +64-4-298-6117 > > > > Richard Rodgers wrote: >> Hello: >> >> I have what I think is a straightforward operation I don't know how to >> accomplish in JiBX - I want to marshall several different objects >> into a single >> document, but the objects may have different marshalling contexts. >> >> For example, the code (where 'out' is an OutputStream) >> >> IBindingFactory bFact = BindingDirectory.getFactory( object.getClass() ); >> IMarshallingContext mCtx = bFact.createMarshallingContext(); >> mCtx.setIndent( 4 ); >> mCtx.marshalDocument( object, "UTF-8", null, out ); >> >> works fine if the document file will contain only the object rooted >> at 'object'; >> but if I have several such objects, where the call to 'getFactory' >> might return >> a different factory, I gather I have to do something like this to build a >> document >> (with Object[] roots ): >> >> 1 // manually construct the XML declaration >> 2 out.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes() ); >> 3 >> 4 // loop thru all objects >> 5 for ( int i = 0; i < roots.length; i++ ) { >> 6 IBindingFactory bFact = BindingDirectory.getFactory( >> roots[i].getClass() >> ); >> 7 IMarshallingContext mCtx = bFact.createMarshallingContext(); >> 8 mCtx.setIndent( 4 ); >> 9 mCtx.setOutput( out, "UTF-8" ); >> 10 >> 11 if ( i < roots.length - 1 ) { >> 12 // this invocation won't call 'endDocument', so I can add >> other objects >> 13 IMarshaller mr = mCtx.getMarshaller( index, >> roots[i].getClass().getName() >> ); >> 14 mr.marshal( object, mCtx ); >> 15 } >> 16 else { >> 17 // this final invocation will call 'endDocument' and >> properly close file >> 18 mCtx.marshallDocument( roots[i] ); >> 19 } >> 20 } >> >> The problem is in line 13: I don't know how to get the correct value for >> 'index'. >> The JavaDoc says 'class index for marshalling definition' but I >> have no idea >> what that means, >> much less how to obtain it programmatically. I searched in vain for sample >> code that works like this. >> >> Any help much appreciated, either in explaining this API, or even >> an alternate >> approach that >> skins the cat another way... >> >> Thanks a lot, >> >> Richard Rodgers >> >> >> ------------------------------------------------------------------------- >> Using Tomcat but need to do more? Need to support web services, security? >> Get stuff done quickly with pre-integrated technology to make your >> job easier >> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo >> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 >> _______________________________________________ >> jibx-users mailing list >> jibx-users@lists.sourceforge.net >> https://lists.sourceforge.net/lists/listinfo/jibx-users >> >> > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share your > opinions on IT & business topics through brief surveys -- and earn cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > _______________________________________________ > jibx-users mailing list > jibx-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/jibx-users > ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ jibx-users mailing list jibx-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jibx-users