• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inserting binary data into an XML Document

 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Long time no see JavaRanch, nice to come back to the ol' Ranch, despite the fact that I can't hang around like I'd like to. Instead, it's only because I'm stuck
Here's the issue, and mind you this is for a class, so no direct answer is appreciated, but instead a point to either an API or some other resource would do.
I'm creating a web service that allows a search of database. The results may yield an object that has associated with it a picture (ie a gif, bmp, or jpg file). What I'd like to do is serialize the graphics file into binary data, and insert that into an XML Document, along with the rest of the normal text data, so that the receiving client will receive the picture upon processing the Document. Sort of like an attachment, but instead actually embedded in an Element. If the client reaches a <Picture> element, it could parse it and display the graphic.
Is this possible? I've thought about having a Picture element return a Boolean and a String value of the URL where the file could be located. If the Boolean is true, the client would make another call to the service (or a new service, whichever), and I would just have the service return the file and let SOAP handle the details, but that's an "easy" way out.
Thanks for any help that is given, hope this makes sense!
Jason
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason,
If the client application is parsing your xml document using say DOM parser, whynot try something like shown below -
<root>
<Picture
location="http://www.jason.com/class.jpg"/>
<Picture
someother="http://www.jason.com/test.jpg"/>
</root>
Now you can look for the "location" attribute for the picture element (which serves as an indicator that the element has the picture information along with it) and then retrieve the attribute value in your client application and then the application can decide how to load the image from the mentioned location.
I'm giving some sample DOM API code implemented in Java here. You can see
http://java.sun.com/j2se/1.4.1/docs/guide/plugin/dom/org/w3c/dom/package-summary.html
for more information.
=================================================
You can have a simple example file (aa.xml) like this -
<root>
<Picture
location="http://www.jason.com/class.jpg"/>
<Picture
someother="http://www.jason.com/test.jpg"/>
</root>
==================================================
You need to have the JAVA DOM API implementation class files on your m/c in order to test the following code working. You can get an idea of how things need to be handled from the following code -
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.io.File;
import org.w3c.dom.*;
public class order{
public static void main (String args[]) {
File docFile = new File("aa.xml");
Document doc = null;
NamedNodeMap nnMap = null;
int i;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docb = dbf.newDocumentBuilder();
doc = docb.parse(docFile);
Element root = doc.getDocumentElement();
NodeList nList = root.getElementsByTagName("Picture");
System.out.println("No. of Picture elements "+ nList.getLength());
// loop on the Picture elements for the location attribute
for(i=0; i<nList.getLength(); i++)
{
String s = ((Element)nList.item(i)).getAttribute("location");
if(s.trim().length()>0)
System.out.println("Picture Element: location :: "+s);
}
}
catch (DOMException de)
{
System.out.println("DOM Exception ::"+de.toString());
}
catch (Exception e)
{
System.out.println("EXCEPTION::"+e.toString());
System.out.println(e.getClass());
}
}
}
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's basically what I was eluding to in the last part of my post (forget the second call to the service, don't know why I was thinking you would have to make another call, versus just going out to the URL and getting the picture).
What I was hoping to do was make it so that the picture could be displayed without having to go to some other resource. Since this is a web service, it's pretty much assumed that the client would be able to reach the picture's URL, but I was trying to make it so a client wouldn't have to, they could just take the binary data and recreate the picture itself. Just seemed like that would make any client able to show the picture, no matter what the situation may be.
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was once told that sending image as a binary data in xml,retrieving back and displaying is not a easy process.
Its also not advisable for your requirement,but you can give a try if you want by referring this article http://www-106.ibm.com/developerworks/xml/library/x-binary/?dwzone=xml
Regards
Balaji
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Balaji,
That was pretty useful information. I think what Jason was looking for is the third option of having the image data directly as part of the CDATA section in his XML document.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From http://www.ibiblio.org/xml/books/bible2/chapters/ch24.html -

Binary types
It's impossible to include arbitrary binary files in XML documents because they might contain illegal characters such as a form feed or a null that would make the XML document malformed. Therefore, any such data must first be encoded in legal characters. The W3C XML Schema Language supports two such encodings, xsd:base64Binary and xsd:hexBinary.
Hexadecimal binary encodes each byte of the input as two hexadecimal digits — 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0A, 0B, 0C, 0D, 0E, 0F, 10, 11, 12, and so on. Thus, an entire file can be encoded using only the digits 0 through 9 and the letters A through F. (Lowercase letters are also allowed, but uppercase letters are customary.) On the other hand, each byte is replaced by two bytes so this encoding doubles the size of the data. It's not a very efficient encoding. Hexadecimal binary encoded data tends to look like this:
A4E345EC54CC8D52198000FFEA6C807F41F332127323432147A89979EEF3
Base64 encoding uses a more complex algorithm and a larger character set, 65 ASCII characters chosen for their ability to pass through almost all gateways, mail relays, and terminal servers intact, as well as their existence with the same code points in ASCII, EBCDIC, and most other common character sets. Base64 encodes every three bytes as four characters, typically only increasing file size by a third, so it's somewhat more efficient than xsd:hexBinary. Base64 encoded data tends to look something like this:
6jKpNnmkkWeArsn5Oeeg2njcz+nXdk0f9kZI892ddlR8Lg1aMhPeFTYuoq3I6n BjWzuktNZKiXYBfKsSTB8U09dTiJo2ir3HJuY7eW/p89osKMfixPQsp9vQMgzph6Qa lY7j4MB7y5ROJYsTr1/fFwmj/yhkHwpbpzed1LE=
XML Digital Signatures use Base64 encoding to encode the binary signatures before wrapping them in an XML element.
Caution
I really discourage you from using either of these if at all possible. If you have binary data, it's much more efficient and much less obtuse to link to it using XLink or unparsed entities rather than encoding it in Base64 or hexadecimal binary.


Cheers,
Dan
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think what Jason was looking for is the third option of having the image data directly as part of the CDATA section in his XML document.


I don't think you can hold binary data in the CDATA sections. Only raw _character_ data can be there.
Cheers,
Dan
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. I tried opening a jpg file and pasted the content in the CDATA section. The validator didn't like it. Does this mean that we have to somehow convert these kinds of jpeg format files into the acceptable raw character data format?? Please clarify.
Thanks.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jayadev,
We need to convert the contents of the binary file to text encoded as hexBinary or base64Binary, preferably the latter. This text can be sent in XML inside the tag defined in the schema to be of type xsd:base64Binary. The receiving end should decode this text and restore the binary. All of this is theory, I never saw it working.
BTW, we can place all this text in a CDATA section, but it doesn't make much sense. It belongs to this specific tags - The validator can verify that they indeed contain the right characters and certain application might even automatically decode this text and display it.
Cheers,
Dan
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dan for the clear explanation
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's basically how .NET does it I believe. From the example I have seen, you first have to turn the file into a byte array, and put it into a structure. Was looking for something similar in the Java API's, like JDOM. Still not 100% sure of what I'm doing, but I'll check out the other methods advised. This is a totally arbitrary service, so no it isn't a requirement. I just wanted to see what I could get away with
Thanks for the info y'all, as always this is the best place to come!
Jason
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic