cancel
Showing results for 
Search instead for 
Did you mean: 

Inbound - get a file into a xml (base64)

Former Member
0 Kudos

Hi experts,

I am trying get a zip file in base64 format that is into of this xml.

<?xml version="1.0" encoding="UTF-8" ?>

- <ns0:mt_csa_source xmlns:ns0="urn:empresa.com:OU030:csa-fi">

- <rset>

- <row>

<FIELD>UEsDBAoAAAAAANZzOUJ1Y8G+GwAAABsAAAAKAAAAYXR0YWNoLnR4dEluZm9ybWF6aW9uZQ0KSW5mb3JtYXppb25lMlBLAQI/AAoAAAAAANZzOUJ1Y8G+GwAAABsAAAAKACQAAAAAAAAAIAAAAAAAAABhdHRhY2gudHh0CgAgAAAAAAABABgAwHajjgj7zQHAdqOOCPvNARiqbMkQ+s0BUEsFBgAAAAABAAEAXAAAAEMAAAAAAA==</FIELD>

</row>

</rset>

</ns0:mt_csa_source>

I can get the content and I decoded it and I put into a byte array, but the logs show me this error:

Adapter Framework caught exception: Exception in XML Parser (format problem?):'org.xml.sax.SAXParseException: Character reference "&#3" is an invalid XML character.'

I tryed chand the xml version for 1.1, but this show me another error

Someone can help me?

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

former_member181985
Active Contributor
0 Kudos

Hi Devaney,

After reading base64 content from XML and then decoding to binary (bytes) to which message structure you are passing this information? I mean, are you using graphical mapping with UDF which decodes base64 to bytes for target XML tag?

XML parsers can not handle binary content in XML tags. So, use java/ABAP mapping to achieve your requirement.

By the by, I can see the below content from your base64 zip content,

Informazione

Informazione2

Regards,

Praveen Gujjeti

Former Member
0 Kudos

Ok, I understood and before I do this code, but in the end the whole process make two file, one is my file zip and the other one is a txt file with the base64 content:

UEsDBAoAAAAAANZzOUJ1Y8G+GwAAABsAAAAKAAAAYXR0YWNoLnR4dEluZm9ybWF6aW9uZQ0KSW5mb3JtYXppb25lMlBLAQI/AAoAAAAAANZzOUJ1Y8G+GwAAABsAAAAKACQAAAAAAAAAIAAAAAAAAABhdHRhY2gudHh0CgAgAAAAAAABABgAwHajjgj7zQHAdqOOCPvNARiqbMkQ+s0BUEsFBgAAAAABAAEAXAAAAEMAAAAAAA==

And I don't now why.

<code>

   File f1 = new File(pathDest + File.separator + fileName);

   FileOutputStream fos=null;
   try {
    fos = new FileOutputStream(f1);
    fos.write(decodedBinary);
    audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, mVar
      + " The file path is "
      + f1.getAbsolutePath());
    audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, mVar
      + " The file name is "
      + fileName);
   } catch (FileNotFoundException e) {
    audit.addAuditLogEntry(key, AuditLogStatus.ERROR, mVar
      + " " + e.getMessage());
   } catch (IOException e) {
    audit.addAuditLogEntry(key, AuditLogStatus.ERROR, mVar
      + " " + e.getMessage());
   }finally{
    try {
     fos.close();
    } catch (IOException e) {
     audit.addAuditLogEntry(key, AuditLogStatus.ERROR, mName
       + e.getMessage());
    }
   }

</code>

Thanks

former_member181985
Active Contributor
0 Kudos

Hi Devaney,

Have you developed a custom module to achieve your requirement? Second thing is that, you are using file I/O in module code. So obviously two files will be created. One is with File I/o code in your java module code, the second one is with File receiver channel.

I do not think you need a custom java module to do this. You can do this with java mapping (at mapping level).

By the by, What is your PI version?

Regards,

Praveen Gujjeti

Former Member
0 Kudos
I implemtn the SessionBean and into the process method I call the receiver process or sender process.
Into the receiver process I have this code:
private Message receiveProcess(Message msg,
   MessageKey key,
   String encoding,
   String fileName,
   String fileNameKey,
   String pathDir)
   throws ModuleException {
 
  // extract payload
  byte by[] = msg.getDocument().getContent();
  byte[] newContent = null;
  XMLPayload xmlpayload = msg.getDocument();
 
  // *** Switch on encoding types *** //
  // Base64
  if (encoding.equalsIgnoreCase("base64")) {
  
   byte[] decodedBinary = null;
   String decodedText = null;
   DocumentBuilderFactory factory;
   factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder=null;
   Document document=null;
   try {
    builder = factory.newDocumentBuilder();
    document = builder.parse(
      (InputStream) xmlpayload.getInputStream());
   } catch (ParserConfigurationException e) {
    audit.addAuditLogEntry(key, AuditLogStatus.ERROR, M_NAME
      + e.getMessage());
   } catch (SAXException e) {
    audit.addAuditLogEntry(key, AuditLogStatus.ERROR, M_NAME
      + e.getMessage());
   } catch (IOException e) {
    audit.addAuditLogEntry(key, AuditLogStatus.ERROR, M_NAME
      + e.getMessage());
   }
  
   Element root = document.getDocumentElement();
  
   Node child = root.getChildNodes().item(0);
   while (child.hasChildNodes()){
    child = child.getChildNodes().item(0);
   }
  
   /* Decode Base64 in String*/
   decodedBinary = Base64.decode(child.getNodeValue());
   decodedText = new String(decodedBinary);
   audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, M_NAME
     + "This is the content "
     + decodedText);
  
   /*
   TransformerFactory tfactory = TransformerFactory.newInstance();
   Transformer transformer;
   try {
    transformer = tfactory.newTransformer();
    DOMSource src = new DOMSource(docNew);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamResult dest = new StreamResult(baos);
    transformer.transform(src, dest);
    newContent = baos.toByteArray();
   
    Result dbg =
     new StreamResult(
       new File("/usr/sap/mydirectory/tmp/Sample.zip"));
    transformer.transform(src, dbg);
   
   } catch (TransformerConfigurationException e){
    audit.addAuditLogEntry(key, AuditLogStatus.ERROR, mName
      + " " + e.getMessage());
   }catch (TransformerException e) {
    audit.addAuditLogEntry(key, AuditLogStatus.ERROR, mName
      + " " + e.getMessage());
   }
   */
  
   File f1 = new File(pathDir + File.separator + fileName);
   FileOutputStream fos=null;
   try {
    fos = new FileOutputStream(f1);
    fos.write(decodedBinary);
   } catch (FileNotFoundException e) {
    audit.addAuditLogEntry(key, AuditLogStatus.ERROR, M_NAME
      + " " + e.getMessage());
   } catch (IOException e) {
    audit.addAuditLogEntry(key, AuditLogStatus.ERROR, M_NAME
      + " " + e.getMessage());
   }finally{
    try {
     fos.close();
    } catch (IOException e) {
     audit.addAuditLogEntry(key, AuditLogStatus.ERROR, M_NAME
       + e.getMessage());
    }
   }
  } else
  // Zip
   if (encoding.equalsIgnoreCase("zip")) {
    //ZIP ALL FILES AND SEND IT
   }
  // Change old message content with the new one
  try {
   if (newContent != null) {
    xmlpayload.setContent(newContent);
   }
   audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, M_NAME
     + "Content was correctly dencoded "
     + encoding.toLowerCase());
  } catch (Exception e) {
   ModuleException me = new ModuleException(e);
   throw me;
  }
  return msg;
}
the return Message I use this way:
inputModuleData.setPrincipalData(msg);
The PI verison is 7.11
I don't know where I did the mistake.
Thank you

Answers (0)