cancel
Showing results for 
Search instead for 
Did you mean: 

How to change an invalid xml

h_deleeuw
Explorer
0 Kudos

Hi,

I have an issue with a file which we do not receive as a valid xml.

A simplified version of the file looks like this:

<line><fieldx value="xxx"/><fieldy value="yyy"/></line>

<line><fieldx value="xxx"/><fieldy value="yyy"/></line>

<line><fieldx value="xxx"/><fieldy value="yyy"/></line>

All I need to make it a valid xml is a root node wrapped around it, like:

<message>

<line><fieldx value="xxx"/><fieldy value="yyy"/></line>

<line><fieldx value="xxx"/><fieldy value="yyy"/></line>

<line><fieldx value="xxx"/><fieldy value="yyy"/></line>

</message>

I have been struggling with xslt, but despite numerous examples, did not get it to work.

We still feel I should work with xslt, but how?

Any suggestions are more then welcome

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Henk,

                 Please do not use FCC in sender channel. Use dummy source and target Interfaces. Apply this java mapping code


import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

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 ValidXML  extends AbstractTransformation{

  /**

  * @param args

  * @throws FileNotFoundException

  * @throws StreamTransformationException

  */

  @Override

  public void transform(TransformationInput arg0, TransformationOutput arg1)

  throws StreamTransformationException {

  // TODO Auto-generated method stub

  execute(arg0.getInputPayload().getInputStream(),arg1.getOutputPayload().getOutputStream());

  }

    public void execute(InputStream in, OutputStream out) throws StreamTransformationException

    {

    try

    {

    StringBuilder s = new StringBuilder();

       byte[] b=new byte[in.available()];

       if(in.read(b) >= 0) {

        s = new StringBuilder(s.toString()+new String(b));

       }

      

      

       s= new StringBuilder(s.insert(0,"<message>"));

       s= new StringBuilder(s.append("</message>"));

      

       System.out.println(s);

       out.write(s.toString().getBytes());

       in.close();

       out.close();

    }   

    catch(Exception e)

    {

    e.printStackTrace();

    throw new StreamTransformationException(e.getMessage());

    }

    }

}

Regards

Anupam

h_deleeuw
Explorer
0 Kudos

Hi,

Thx for your reply, it looks like a good solution.

We will try this monday, I'll let you know what the result is.

Regards.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Henk,

             All the best. You will definately succeed.

Regards

Anupam

Answers (3)

Answers (3)

h_deleeuw
Explorer
0 Kudos

Hi Anupam,

I have applied this code in the Attributes and Methods section of the message mapping and it works perfectly.

Thanks for your help.

Regards,

Henk

anupam_ghosh2
Active Contributor
0 Kudos

Thank you Henk for proving an update, closing the thread and marking my answer as correct one.I am really happy that the code worked.

Regards

Anupam

iaki_vila
Active Contributor
0 Kudos

Hi Henk,

Another alternative if you are using a file adapter is to create a shell script ( Defining Operating System Commands Before/After Processing - Advanced Adapter Engine - SAP Library) to add the two tags for the root node before processing.

Regards.

h_deleeuw
Explorer
0 Kudos

Hi, thx for your reply.

The option of an OS script was also discussed within our team, but we prefer to solve it in a mapping.

But this will probably the way we go when that doesn't work.

Regards.

engswee
Active Contributor
0 Kudos

Hi Henk

AFAIK, XSLT requires a valid XML as an input which you do not have. I think your only option would be to go for a Java mapping to add the root node to the payload.

Rgds

Eng Swee