JAXB - why can i only marshal to a stream/file?

2.4k views Asked by At

i´m currently working on a project, with following architecture:

project A (shall storage files into a folder. Shall take byte[] and name and should write the data in a file. Not more, just a dumb file-writer application)

project B (shall read data from database, generates a xml file of it and sends that as byte[] to project A, which writes this in filesystem)

Now my problem: In project B, i want to generate the xml-file as an object, but the API of the Jaxb-Marshaller gives only marshal() methods, which takes as argument a Writer/File/OutputStream...And i don´t want to give a File or OutputStream in the project B- this should be task of Project A...

So why is there no method which returns the generated XML as Object or as byte[] or else?

2

There are 2 answers

0
user3707125 On

Pass in ByteArrayOutputStream. Then you can convert it to byte[] using toByteArray()

0
Alex On

The point of the marshal() method asking for streams is exactly to give you the opportunity to decide how your output should be. You can stream the output to whatever you like. You can stream it to memory (using for example the ByteArrayOutputStream or StringWriter as suggested by others), to the file system, through a network socket or even let a client from your API decide what to do with it by letting it pass its own Stream.

So to answer your question, the reason why marshal() demands an OutputStream or Writer only is because that's actually more than enough. :)