Skip to Content
0

Java or XSLT mapping suits this requirement

Apr 12 at 02:47 PM

73

avatar image

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.

req.jpg (96.5 kB)
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Evgeniy Kolmakov Apr 13 at 09:13 AM
1

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.

Show 2 Share
10 |10000 characters needed characters left characters exceeded

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.

0

Hi Ramu!

Regards, Evgeniy.

0
Che Eky Apr 12 at 04:08 PM
0

I would try with XSLT first.

Share
10 |10000 characters needed characters left characters exceeded
Evgeniy Kolmakov Apr 12 at 06:22 PM
0

Hi Ramu!

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

Regards, Evgeniy.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

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.

0