Recent

Author Topic: [SOLVED] Save XML Attachment as binary file  (Read 1766 times)

Kjooow

  • Jr. Member
  • **
  • Posts: 90
[SOLVED] Save XML Attachment as binary file
« on: February 23, 2019, 12:51:14 pm »
I all,

I have an XML with a TAG "Attachment" that contains a binary file (in my case a PDF), part of xml is:

Code: XML  [Select][+][-]
  1. <tag1>
  2.   <tag2>
  3.          <subtag1>123</subtag1>
  4.          <AttachmentType>PDF</AttachmentType>
  5.          <Attachment>JVBERi0xLjQKJeLjz9MKMiAwIG9iagpbL1BERi9UZXh0L0ltYWdlQi9JbWFnZUMvSW1hZ2VJXQpl
  6. bmRvYmoKNCAwIG9iago8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvTGVuZ3RoIDE1NzQ+PnN0cmVhbQp4
  7. 2pVZy47jNhC8z1fouHtYWCIp0QIGBoIEAeaQBzK5BXvQgxoYyMiGx3OYv4+trra7DI4XWWCXS1l8
  8. [...]
  9.          </Attachment>
  10.   </tag2>
  11. </tag1>
  12.  

I can read everything with units laz2_XMLRead e laz2_DOM, but I don't know how to save the binary data.

My approach is:

Code: Pascal  [Select][+][-]
  1.  
  2.   TForm1 = class(TForm)  
  3. [...]
  4.   private                                      
  5.     Attachment: TFileStream;      
  6.   public
  7.   end;                  
  8.  
  9. [...]
  10.  
  11.   Attachment:= TFileStream.Create('myfile.pdf',fmCreate);
  12.   Attachment.WriteAnsiString(AttachmentXML.ChildNodes[0].NodeValue); //where AttachmentXML is a TDOMNode (filled right)
  13.   Attachment.Free;                                                          
  14. [...]
  15.  

This code generates a file with the text in Attachment, not a binary readable from a PDF reader...

This XML works with other integrated software, at work. I need to manage a lot of xml files, so I need to develop a sw to extract data and binary pdf automatically.

Thank you!
« Last Edit: February 23, 2019, 07:01:05 pm by Kjooow »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Save XML Attachment as binary file
« Reply #1 on: February 23, 2019, 05:59:16 pm »
I can confirm that is a base64 encoded binary, so decode it first before saving it as a binary pdf file.

Kjooow

  • Jr. Member
  • **
  • Posts: 90
Re: Save XML Attachment as binary file
« Reply #2 on: February 23, 2019, 06:22:57 pm »
I can confirm that is a base64 encoded binary, so decode it first before saving it as a binary pdf file.

Thank you very much!
It works like a charm  :D

Code: Pascal  [Select][+][-]
  1.  
  2. uses
  3.   [...], base64;
  4.  
  5.   TForm1 = class(TForm)  
  6. [...]
  7.   private                                      
  8.     Attachment: TFileStream;      
  9.   public
  10.   end;                  
  11.  
  12. [...]
  13.  
  14.   Attachment:= TFileStream.Create('myfile.pdf',fmCreate);
  15.   Attachment.WriteAnsiString(DecodeStringBase64(AttachmentXML.ChildNodes[0].NodeValue,false)); //where AttachmentXML is a TDOMNode (filled right)
  16.   Attachment.Free;                                                          
  17. [...]
  18.  

 

TinyPortal © 2005-2018