cancel
Showing results for 
Search instead for 
Did you mean: 

Reading XML payload from text file

Former Member
0 Kudos

Hello,

I have a text file with XML payload in it. The text file has some header information and then the XML payload. How do I read only the XML payload for processing in PI?

the structure of the text file is something like this..

Header Information

Payload

Sample:

#Datetime: 20140318-09:28:50.129

#MessageID: sample123

#Sender: TEST

#SenderParty: TEST

#ReceiverService:

#ReceiverParty: Test

#Interface: Test123

#InterfaceNamespace: http://test.com

#

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

<Sample ><Header>......

Please advise.

Thank you.

Larry.

Accepted Solutions (1)

Accepted Solutions (1)

praveen_sutra
Active Contributor
0 Kudos

hi Larry,

Simplest would be to use a java mapping and fetch the payload part.

Please let me know if you would require java mapping.

thanks and regards,

Praveen T

praveen_sutra
Active Contributor
0 Kudos

package com.learning.utils;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

public class FetchPayload extends AbstractTransformation {

  String strData = null;

  @Override

  public void transform(TransformationInput arg0, TransformationOutput arg1)

  throws StreamTransformationException {

  // TODO Auto-generated method stub

  getTrace().addInfo("File Reading started ");

  String strData = convertStreamToString(arg0.getInputPayload()

  .getInputStream());

  getTrace().addInfo("File Reading successfully completed ");

  try {

  getTrace().addInfo("Generating XML started");

  strData.substring(strData.indexOf("<?xml"), strData.length());

  arg1.getOutputPayload().getOutputStream().write(

  strData.getBytes("UTF-8"));

  getTrace().addInfo("Generating XML started");

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  public String convertStreamToString(InputStream in) {

  StringBuffer sb = new StringBuffer();

  try {

  InputStreamReader isr = new InputStreamReader(in);

  Reader reader = new BufferedReader(isr);

  int ch;

  while ((ch = in.read()) > -1) {

  sb.append((char) ch);

  }

  reader.close();

  } catch (Exception exception) {

  }

  return sb.toString();

  }

}

and u can get  the xml as a resultant which  u can use as input for next message mapping.

thanks and regards,

Praveen T

Former Member
0 Kudos

wonderful! , Thanks for the details. Just had to change it with 

strData = strData.substring(strData.indexOf("<?xml"), strData.length());

and it worked as I expected.

Thanks for your help.

Larry.

Answers (0)