cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Java or XSLT mapping suits this requirement

ramu_g4
Participant
0 Likes
512

Hi Experts,

We have requirement to transform the input & output payload as shown below screen shot where it should parse payload and " < " should be replaced with < & " #62 "should be replaced with " > " .

If you observe the whole xml payload contains ServiceOrder string with set of characters should be replaced with < infront & > after that string.

Please suggest Java or XSLT mapping suits this requirement.

Thanks.

Ramu.

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member190293
Active Contributor

Hi Ramu!

public class StringReplacer extends AbstractTransformation {
@Override
public void transform(TransformationInput in, TransformationOutput out)throws StreamTransformationException {
executeMapping(in.getInputPayload().getInputStream(), out.getOutputPayload().getOutputStream());
}

public void executeMapping(InputStream is, OutputStream os) throws StreamTransformationException {
try {
byte[] payloadBytes = getByteArrayFromInputStream(is);
String payloadString = new String(payloadBytes, "UTF-8");
payloadString = payloadString.replaceAll("<", "<").replaceAll(">", ">");
os.write(payloadString.getBytes("UTF-8"));
}
catch (Exception e) {
throw new StreamTransformationException(e.getMessage());
}
}

public byte[] getByteArrayFromInputStream(InputStream is) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1)
  buffer.write(data, 0, nRead);
buffer.flush();
is.close();
return buffer.toByteArray();
}
}

Editor changes the representation of symbols, so, you should use your source values for "<" and ">" as the first argument in both replaceAll() methods.

Regards, Evgeniy.

ramu_g4
Participant
0 Likes

Hi Evgeniy,

Regarding this line payloadString = payloadString.replaceAll("<", "<").replaceAll(">", ">");

should it corrected to achieve my requirement to replace < with "<" and to replace > with ">" .

payloadString = payloadString.replaceAll("<", "<") ;

payloadString = payloadString.replaceAll(">", ">") ;

Thanks,

Ramu.

former_member190293
Active Contributor
0 Likes

Hi Ramu!

Regards, Evgeniy.

former_member190293
Active Contributor
0 Likes

Hi Ramu!

I would read payload into string using java mapping and simply call replaceAll() method.

Regards, Evgeniy.

ramu_g4
Participant
0 Likes

Hi Evgeniy,

Thank you for your response.

Can you help with Java mapping code to read the payload & use replaeAll() method to achieve this requirement so that I can export the java code as jar file & import it.

Thanks,

Ramu.

che_eky
Active Contributor
0 Likes

I would try with XSLT first.