2

I have been given an assignment to write a program in Java which stores an image into XML and from given XML extract original image.

I have searched a lot over net for the solution but didn't get any proper answer from where I can learn the complete process of transformation, so if anybody know the links or have any sort of material please let me know.

bluish
  • 26,356
  • 27
  • 122
  • 180
DCoder
  • 3,486
  • 7
  • 36
  • 68
  • you have to convert image into bytes and you can store along with Xml . http://stackoverflow.com/questions/7119472/convert-byte-array-byte-to-image-in-java this link will help you to give basic information – Mani Dec 30 '13 at 17:29

3 Answers3

6

You can convert the image bytes to base64, and save the bytes as a string. You would decode the base64 when reading the file back out.

Though bitmaps can be large. I would try to save the file somewhere else and only save the path in XML.

Kevin Seifert
  • 3,494
  • 1
  • 18
  • 14
  • tnx. please tell me any link which demonstrate this process. – DCoder Dec 30 '13 at 17:42
  • You might look at https://gist.github.com/utkarsh2012/1276960 and http://stackoverflow.com/questions/9681239/base64-encoding-a-file-in-java for examples. – Kevin Seifert Dec 30 '13 at 17:45
1

XML is not suited for storing binary data. What it's suited for is storing metadata such as image dimensions, color depth, author, etc. You can just store file name and metadata in the XML and have the binary file separate. In case you absolutely must use XML, then you can Base64 encode the binary in one of the fields.

dimoniy
  • 5,820
  • 2
  • 24
  • 22
1

Some ideas to get you started on your assignment:

  • bitmap bytes can be transformed into a string of hexadecimal values
  • XML can contain large data blocks using CDATA nodes
rsp
  • 23,135
  • 6
  • 55
  • 69