users@jaxb.java.net

Re: how to attach MS word document in xml message?

From: Kohsuke Kawaguchi <kohsuke.kawaguchi_at_sun.com>
Date: Tue, 07 Jun 2005 13:00:54 -0400

Tang, Harry wrote:
> Hi, It seems that there is a way to embed image data files in the xml messages.
> Does anyone know if ms word document can also be embeded as element in xml
> message? I would appreciate if someone can provide a simple example for the
> schema and JAXB process to encode and extract the file from XML. Message.

In 1.0, the way for you to do it is to use base64 binary.
You declare your schema like:

        <xs:element name="document">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="wordDocument" type="xs:base64Binary" />
              ...

And then at the runtime, you read a document into a byte[], and do
        
        byte[] data = ...;
        document.setWordDocument(data);

to marshal it.

Your XML will look like:

        <document>
          <wordDocument>
            aB78asdASD$==
          </wordDocument>
        </document>

You'll have to manually convert between byte[] and actual file. If you
need to send file name, timestamp, and etc, then you need to do it
manually. Also note that the document on the wire is 33% larger because
of the base64 encoding.

In 2.0, you have a few more options. Everything I said in above works,
plus you can use javax.activation.DataHandler. This allows you to declare

        class Data {
                DataHandler wordDocument;
        }

and you can set wrap a FileInputStream or URLDataSource into
DataHandler. I think this is exactly the same as attaching a document in
JavaMail. I realize that this still isn't super-easy, so maybe it would
be nice to have a convenience wrapper class in this jaxb.dev.java.net
project.

Also, when you are actually sending a message, 2.0 can let you use MTOM
(aka XOP). This allows the binary to be sent directly without the file
size increase, but now your "XML" will become a MIME multipart message.


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com