*
The moose likes XML and Related Technologies and the fly likes How to serialize Images using XStream? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to serialize Images using XStream?" Watch "How to serialize Images using XStream?" New topic
Author

How to serialize Images using XStream?

Mimi Tam
Ranch Hand

Joined: Mar 05, 2010
Posts: 121
Hi,

XStream is working great for me. Now I need to serialize/deserialize an image file (e.g. .png, .img, etc) which is inside my XML HTTP message. I am using Hibernate 3.0 (Annotations).

I did some digging for the info but I couldn't find examples or mentionings on how to serialize, and few for deserialize.

Can someone give me some info/examples on how to do them, please?

Currently, I have this code segment:

The returned string was formatted in XML simply as a string. I have *.png files embedded in the XML content and I need to send them as is in binary (not the file name) properly between 2 XML tags and the rest string.

Looking forward to hearing from someone.

Many Thanks...Mimi







Many Thanks...Mimi
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Since you cant include arbitrary binary directly in XML, you must encode it in legal XML characters.

The Apache Commons Codec library includes base64 encoding classes. With Base64 encoding, 3 bytes become 4 characters so the overhead is not too bad.

Bill
Mimi Tam
Ranch Hand

Joined: Mar 05, 2010
Posts: 121
Thank you very much for your response, Bill.

I was thinking more on the line of using @Lob, byte[] for my image fields on my POJOs and hoping that they will be serialized/deserialized to MySQL and become longblob type.

Since you cant include arbitrary binary directly in XML, you must encode it in legal XML characters


Are you referring to the CLOBs and BLOBs and from converting BLOBs to Byte[] and back, that kind of conversion? That is what I have in mind but I am seeking an example of how this is done in a more complete context, instead of 2/3 line code segments.

Sorry, I wasn't too clear.

Any help will be greatly appreciated.


William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Are you referring to the CLOBs and BLOBs and from converting BLOBs to Byte[] and back, that kind of conversion?


No, I a referring to the fact that the byte[] of image data can not be inserted in a SOAP message directly because there are many byte values which are not legal as XML characters.

I have NO idea what facilities Hibernate 3.0 provides so you are on your own there.

Bill
Mimi Tam
Ranch Hand

Joined: Mar 05, 2010
Posts: 121
Thank you so very much for your reply and clarification, Bill. I really appreciate it.

I finally got it working using Hibernate 3 Annotations and XStream. The code required is very little, very clean and concise. It was my oversight of error in my code and I created the problem for myself.

Thanks again...Mimi
 
Don't get me started about those stupid light bulbs.
 
subject: How to serialize Images using XStream?
 
Similar Threads
Cloning a LinkedList?
xmlbeans serialization
Problem with Serialization / Deserialization
xm file as input for servlet
Need XML elements marshalling / unmarshalling example code