Skip to Content

Java or XSLT mapping suits this requirement

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)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Apr 13 at 09:13 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 12 at 04:08 PM

    I would try with XSLT first.

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 12 at 06:22 PM

    Hi Ramu!

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

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed 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.