users@jaxb.java.net

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

From: Tang, Harry <Harry.Tang_at_bellsouth.com>
Date: Tue, 7 Jun 2005 12:56:18 -0500

Hi, Kawaguchi,
  The last paragraph below is what I was originally looking for. But I
did not realize that if using MIME multipart message, it is not strict
XML message anymore.
   The DataHandler approach is interesting, but as you mentioned, it
would be nice it can simplify process to insert the uploaded files to
xml message.

Thanks,

Harry




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
*****
The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. 162