1. Duplicate accounts are absolutely forbidden on our forum! - One person, one account. We have an automated detection system in place, you will be automatically banned or restricted with all Likes removed - no second chances!

Need help to read xml attachments

Discussion in 'HelpDesk' started by tib61, Feb 10, 2019.

  1. ti

    tib61 Registered User

    Joined:
    Sep 17, 2008
    Messages:
    413
    Likes Received:
    2,628
    I need help to read attachments inside xml tag as base64.
    THZ
     
  2. ro

    robrasra Registered User

    Joined:
    Mar 22, 2018
    Messages:
    122
    Likes Received:
    1,003
    Do you have example?
     
  3. ti

    tib61 Registered User

    Joined:
    Sep 17, 2008
    Messages:
    413
    Likes Received:
    2,628
    Yes my example when I did try to decode was this:

    Code:
      Nome_file:=copy(ExtractFilePath(Application.ExeName),1,1)+':\MyProgram\FE\'+FModuloComune.qryA_ANADITTA.FieldByName('SIGLA').AsString+'\IMPORT\'
    
        +trim(FModuloComune.mtblFattENOME_FILE.AsString);
    
      AssignFile(testoFile, Nome_file);
    
      Reset(testoFile);
    
      while not EOF(testoFile) do
    
        begin
    
          ReadLn(testoFile, buffer);
    
          if Pos('<Attachment>', buffer)>0 then
    
            begin
    
              SupAll:=Buffer;
    
              while not EOF(testoFile) do
    
                begin
    
                  ReadLn(testoFile, buffer);
    
                  SupAll:=SupAll+buffer;
    
                end;
    
            end;
    
        end;
    
    Code:
      TIdDecoderMIME.DecodeString(copy(SupAll, pos('<Attachment>', SupAll)+12, pos('</Attachment>', SupAll)-1));
    
    But it didn't decode the base64 between <Attachment> and </Attachment> tag.

    Thanks for Your help.
     
    Last edited by a moderator: Feb 11, 2019 at 12:52
  4. Youpi

    Youpi is a Trusted Warez PosterYoupi Honorary Member Honorary Member DEV Guild

    Joined:
    Jan 1, 1970
    Messages:
    67,017
    Likes Received:
    10,463
  5. op

    opi Registered User

    Joined:
    Oct 5, 2011
    Messages:
    807
    Likes Received:
    683
    Code:

    Code:
    uses
      Xml.XMLDoc, Xml.XMLIntf, System.NetEncoding;
    
    ....
    
    var
      XMLDocument: IXMLDocument;
      XMLNode: IXMLNode;
      AttachmentBase64: string;
      AttachmentBytes: TBytes;
      AttachmentStream: TFileStream;
    begin
      XMLDocument := TXMLDocument.Create(nil);
      try
        XMLDocument.LoadFromFile('X:\Path\To\Your\file.xml');
    
        // 1. add error checking and 2. change this so it finds your attachment node
        XMLNode := XMLDocument.ChildNodes.FindNode('something').ChildNodes.FindNode('Attachment');
    
        AttachmentBase64 := XMLNode.NodeValue;
        AttachmentBytes := TNetEncoding.Base64.DecodeStringToBytes(AttachmentBase64);
        AttachmentStream := TFileStream.Create('X:\Path\To\Attachment\Attachment.bin', fmCreate);
        try
          AttachmentStream.WriteBuffer(AttachmentBytes, Length(AttachmentBytes));
        finally
          AttachmentStream.Free;
        end;
      finally
        XMLDocument.Active := False;
        XMLDocument := nil;
      end;
    end;
    

    Test xml file:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <something>
      <Attachment>dGhpcyBpcyBhIHRlc3Q=</Attachment>
    </something>