Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert images to XML file

Tags:

xml

image

I would like to insert an images within an element the XML file, what is the best way to do this? Would you please suggest some good way to include an images to xml file?

like image 624
Nguyen Quoc Hung Avatar asked Apr 14 '09 08:04

Nguyen Quoc Hung


3 Answers

The most common way of doing this is to include the binary as base-64 in an element. However, this is a workaround, and adds a bit of volume to the file.

For example, this is the bytes 00 to 09 (note we needed 16 bytes to encode 10 bytes worth of data):

<xml><image>AAECAwQFBgcICQ==</image></xml>

how you do this encoding varies per architecture. For example, with .NET you might use Convert.ToBase64String, or XmlWriter.WriteBase64.

like image 125
Marc Gravell Avatar answered Oct 13 '22 06:10

Marc Gravell


Since XML is a text format and images are usually not (except some ancient and archaic formats) there is no really sensible way to do it. Looking at things like ODT or OOXML also shows you that they don't embed images directly into XML.

What you can do, however, is convert it to Base64 or similar and embed it into the XML.

XML's whitespace handling may further complicate things in such cases, though.

like image 5
Joey Avatar answered Oct 13 '22 06:10

Joey


XML is not a format for storing images, neither binary data. I think it all depends on how you want to use those images. If you are in a web application and would want to read them from there and display them, I would store the URLs. If you need to send them to another web endpoint, I would serialize them, rather than persisting manually in XML. Please explain what is the scenario.

like image 5
Slavo Avatar answered Oct 13 '22 04:10

Slavo