Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
mani1304
Creator
Creator

pdf file attachment into XML message

Hi Team,

I want to attach a pdf file into an XML message to push into queue, can someone suggest on it, I got one piece of code to first convert it into base64 then to push in java but not getting on how to implement it or some other way.

import java.io.File;

import java.nio.file.Files;

import java.util.Base64;

class Test {

 public static void main(String[] args) {

  try {

   File file = new File("C:\\\\Users\\\\ngargp8\\\\Desktop\\Power DataSync.pdf");

   byte [] bytes = Files.readAllBytes(file.toPath());

   String b64 = Base64.getEncoder().encodeToString(bytes);

   System.out.println(b64);

  } catch (Exception e) {

   e.printStackTrace();

  }

 }

}

Labels (3)
8 Replies
Anonymous
Not applicable

In talend, you can hard code on tJava or write a user routine to implement the function.

For example, on tJava:

in the basic setting panel:

try {

  File file = new File("D:/shong/files/3.txt");

  byte [] bytes = Files.readAllBytes(file.toPath());

  String b64 = Base64.getEncoder().encodeToString(bytes);

  System.out.println(b64);

 

 } catch (Exception e) {

  e.printStackTrace();

 }

in the advanced settings panel.

import java.io.File;

import java.nio.file.Files;

import java.util.Base64;

 

 

Regards

Shong

mani1304
Creator
Creator
Author

Thanks @Shicong Hong​ , but I think from this way I can convert it to Base64, but how can I attach it with an XMl message. something like tUnite or something else.

Anonymous
Not applicable

You can store the value to a context variable for used on other component later .

try {

  File file = new File("D:/shong/files/3.txt");

  byte [] bytes = Files.readAllBytes(file.toPath());

  String b64 = Base64.getEncoder().encodeToString(bytes);

context.varName=b64;

  System.out.println(b64);

 

 

 } catch (Exception e) {

  e.printStackTrace();

 }

mani1304
Creator
Creator
Author

Thanks @Shicong Hong​ , but sorry I didn't get it, how can I add this in my xml fie, I have created an XML file via tfileOutputMSXML via merging 4 files flow, now I want to add this pdf into it also. I will take this pdf's base64 value in context variable, not how can I add it in my XML file.

Anonymous
Not applicable

Before tfileOutputMSXML, use a tMap and set the expression of the field as context.varName on tMap, this field will be assigned to XML element.

0693p00000BBoePAAT.png 

mani1304
Creator
Creator
Author

Thanks @Shicong Hong​ , if I am getting it correctly you're saying to add this context value in one of the output of tmap, then merge all 4 rows like the attachment, but then it would be a sub-element of one of the file, but I want it as one separate tag, say pdf like

<A></A>

<B></B>

<C></C>

<D></D>

</pdf></pdf>

Currently I don't have output file xsd, I am only doing a POC.

mani1304
Creator
Creator
Author

@Shicong Hong​ Could you please suggest. I have taken that pdf into trowgenerator then tMap and assigning the value into it, Is it good or any better option for this.

Anonymous
Not applicable

My recommendation is writing a routine which accepts the file path and returns the base64 encoded String.