• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1261
  • Last Modified:

Write Binary Data to XML file

Hi Experts I want to write binary data to XML file. I dont want to write data from database to xml file but actually i want to write an object's data to XML file. I want help from you people to write object data to XML file. This object can be of an class or of an structure.

Thnx in advance.
1
tamoori
Asked:
tamoori
  • 10
  • 5
1 Solution
 
Eddie ShipmanAll-around developerCommented:
Per the XML Specification, encode the binary data as base-64 and enclose it in a CData Section

You can see how in the ObjectBinaryToXML method in Jeff Rafter's XMLObjects here:
http://www.delphipages.com/result.cfm?ID=4888
0
 
tamooriAuthor Commented:
When i try  to write my objets's data in the memory stream using Write method of TMemoryStream and then Call ObjectBinarytoText method then it throws an exception of
"Invalid Stream Format". I dont want to write any component's data in XML file. Actually i want to write Object's data in XML file. Please help me out in this matter. I will be very thankful to you.
0
 
Eddie ShipmanAll-around developerCommented:
I only said to use the same techniques used in ObjectBinaryToXML. It isn't difficult
to translate the usage to convert a binary stream to XML. I'll see what I can come up with.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tamooriAuthor Commented:
Where are you guys. Please help me in this problem. I am still waiting for the reply from experts. Please do reply with comprehensive solution as soon as possibel.
0
 
Eddie ShipmanAll-around developerCommented:
Sorry, I forgot.
Give me one more day.
0
 
Eddie ShipmanAll-around developerCommented:
What DOM are you using?
0
 
Eddie ShipmanAll-around developerCommented:
OK, here is some code that works with the Microsoft XML DOM (MSXML)
and Indy9's TidEncoderMIME:

uses ..., MSXML2_TLB, IdBaseComponent, IdCoder, IdCoder3to4, IdCoderMIME;

procedure AddBinaryFile(AStream: TStream; oNode: IXMLDOMNode);
var
  oCDataSection: IXMLDOMCDATASection;
  meMIMEEncoder: TIdEncoderMIME;
begin
  meMIMEEncoder := TIdEncoderMIME.Create(nil);
  try
    oCDataSection := oXMLDoc.createCDATASection(meMIMEEncoder.Encode(AStream, AStream.Size));
  finally
    meMIMEEncoder.Free;
  end;
  oNode.appendChild(oCDataSection);
  oCDataSection := nil;
end;


I ran into a problem with the Encoder and am tracking the problem down now.
It involves the filesize of the stream being invalid if not divisible by 3, don't even ask...

This function works with some streams at this time. I will post an update once I get
more info on it.

0
 
Eddie ShipmanAll-around developerCommented:
Ok, the "bug" in the TIdEncoder3to4.Encode function has purportedly
been fixed in Indy9. I forgot that I did not have Indy9 installed but the
default Indy that came with D7. I have to reinstall D7 recently.

Open IdCoder3to4.pas and look at that function to see if you see this code:

  if (ABytes <> MaxInt) and ((ABytes mod 3) > 0) then begin
    raise EIdException.Create(RSUnevenSizeInEncodeStream);
  end;


If it is there, go here http://tinyurl.com/kzaw2 (Borland newsgroup posting)
and get the updated code from Remy Lebeau's post and recompile your
Indy version.

Or, you can use other Base64 encode routine and rework my AddBinaryFile
function to use it.
0
 
tamooriAuthor Commented:
I appreciate your effort but please provide me code of Delphi IDE. I am trying to use this code example in my project . Meanwhile you try to send me code written in delphi or c++ builder.

thnx in advance.
0
 
Eddie ShipmanAll-around developerCommented:
What the heck do you think that code is? It was written using Delphi7.

You must have the Indy controls installed AND import ther Microsoft XML
Type Library, other than that, it is straight Delphi.

Are you saying you want the code for the whole example?
0
 
tamooriAuthor Commented:
Dont be angry my friend. I dont want code for whole example. I am doing project in Borland C++ Builder 6 and want to know a method in this IDE which converts binary data into base64 with size of binary data. u can help me in full by telling me method name if it exists otherwise tell me an alternative procedure.

thnx in advance.
0
 
Eddie ShipmanAll-around developerCommented:
I'm not angry, just confused as to why you are asking for Delphi code, when that is what I posted.
If you are doing this project in BCPP6, then you should have said so in your post and, better yet, should
have posted this in the C++ topic area.

There is none in the IDE, you MUST use a third-party utility, such as Indy's idEncoderMIME, to do it.
There may be other C++ base64 utilites out there that you can find using google or one of the
552 results found for 'base64' at http://www.koders.com



0
 
Eddie ShipmanAll-around developerCommented:
I will try to get a BCPP6 conversion of the source above for you.
0
 
tamooriAuthor Commented:
I am waiting for your reply.
0
 
Eddie ShipmanAll-around developerCommented:
Here is the reply from Remy Lebeau:

    #include "msxml2.h"
    #include "IdBaseComponent.hpp"
    #include "IdCoder.hpp"
    #include "IdCoder3to4.hpp"
    #include "IdCoderMIME.hpp"
    #include <utilcls.h>

    void __fastcall AddBinaryFile(TStream *AStream, IXMLDOMNode *oNode)
    {
        TComInterface<IXMLDOMCDATASection> oCDataSection;
        TComInterface<IXMLDOMNode> oChild;

        TIdEncoderMIME *meMIMEEncoder = new TIdEncoderMIME(NULL);
        try
        {

oXMLDoc->createCDATASection(WideString(meMIMEEncoder->Encode(AStream,
AStream->Size)), &oCDataSection);
        }
        __finally
        {
            delete meMIMEEncoder;
        }

        oNode->appendChild(oCDataSection, &oChild);
    }

> Does Indy come installed with BCPP6?

Indy 8 does.  That is a very old version, though.  You would have to
completely remove it from the computer first in order to upgrade to Indy 9
or higher.


0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now