cancel
Showing results for 
Search instead for 
Did you mean: 

Split payload

former_member581827
Participant
0 Kudos

Hi,

can you help me in getting UDF for splitting payload, The payload gets into one of the field and i need to check if legth of the field is greater than 255 i need to take down to next line.

If next line is also greater than 255 then need to split next part of 255 into another line.

I tried using this UDF and its not workning.

for(;ln>255;pos= pos+255)

{

result.addValue(field[0].substring(pos,pos+255));

if (ln>255)

{

ln=ln-255;

}

result.addValue(field[0].substring(pos));

Thanks,

CH

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Chk Greg's reply in below thread:

http://scn.sap.com/thread/3160240

Thanks

Amit Srivastav

former_member581827
Participant
0 Kudos

can you please correct me in my UDF which i have tried like

for(;ln>255;pos= pos+255)

{

result.addValue(field[0].substring(pos,pos+255));

if (ln>255)

{

ln=ln-255;

}

result.addValue(field[0].substring(pos));

former_member184681
Active Contributor
0 Kudos

Hi Chandra,

You don't need two loop control variables (ln and pos), one is enough and it can be controlled by the loop itself. Moreover, you cannot exceed the size of the string with the substring function, since it causes an error. Finally, make sure your input is called "field" and it only has one value - if not, you would need one more loop around your code.

Try with this modification to your original code:

for (int pos = 0; pos < input.length(); pos += 255){

     int len = 0;

     if (pos + 255 < input.length()) len = 255;

     else len = input.length() - pos;

     result.addValue(field[0].substring(pos, pos + len));

}

Regards,

Greg

former_member581827
Participant
0 Kudos

I am getting empty value.

former_member184681
Active Contributor
0 Kudos

Hi Chandra,

I can see some inconsequence in my code, the input variable is called field once, and input other times. You have to adjust it according to the input variable name. My suggestion is to set the execution type: single values, call the input parameter input and use exactly the following code:

for (int pos = 0; pos < input.length(); pos += 255){

     int len = 0;

     if (pos + 255 < input.length()) len = 255;

     else len = input.length() - pos;

     result.addValue(input.substring(pos, pos + len));

}

Regards,

Greg

former_member581827
Participant
0 Kudos

it worked fine but i need the the header node also to be replicated the number of likes like how many it splits.

suppose the XML payload length is 1020 which it has to be splitted into 4 lines in target field.

which is 1020/255 = 4

My target structure is <Line> 0.... Unbounded and in that <Text> is 0 to 1.

So the expected result is some thing like <Line>

                                                                 <Text>

                                                            <Line>

                                                                  <Text>

                                                            <Line>

                                                                   <Text>

                                                             <Line>

                                                                    <Text>

By udf i am getting only one  which is   <Line>

                                                               <Text>

former_member184681
Active Contributor
0 Kudos

Hi,

This is simple - just map the result of the XML to both: Line and Text, like this:

source -> UDF -> Line

source -> UDF -> SplitByValue(Each Value) -> Text

Regards,

Greg

Former Member
0 Kudos

Removing my post as solution is already provided above

Message was edited by: Amit Srivastava

Answers (3)

Answers (3)

baskar_gopalakrishnan2
Active Contributor
0 Kudos

You can follow Greg's code already pointed by Amit's reply. In this case change your size = 255 and rest are all pretty much same.  Dont forget to use splitbyvalue function after UDF as shown in the screenshot of that thread  http://scn.sap.com/thread/3160240

@Inaki: Just few cents... array length is specified without parenthesis ()

Hope that helps.

iaki_vila
Active Contributor
0 Kudos

Hi Baskar,

When  i wrote the parenthesis is for the string length (fieldSource[i].length()), pay attention to the brackets [].

I suppose an array with multiples strings, for different source elements that it will be split in only one step (all values in queue).

I made a "for" for the arrays of strings and another "for" for the string. May be the string could be parsed more efficiently.

Regards.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Yes, You are right. I did not see the brackets on your second for block.

iaki_vila
Active Contributor
0 Kudos

Hi Chandra,

Another aproximation, with mode all values in queue:

public void splitField(String[] fieldSource,ResultList result,Container container){ 

int k = 0;

   String stringConcatenated = "";

    for (int i = 0; i < fieldSource.length; i++){

      for (int j = 0; j < fieldSource[i].length(); j++){ 

       if  ((k < 254) && (j < fieldSource[i].length() -1 )){    

              stringConcatenated += fieldSource[i].charAt(j);

               k = k + 1; }

      else {

           stringConcatenated += fieldSource[i].charAt(j);

           result.addValue(stringConcatenated);

           result.addContextChange();

            k = 0 ;

            stringConcatenated = "";       }

    }

   

}

}

If you have multiple fields to split, it could be useful for you.

Regards.

former_member581827
Participant
0 Kudos

I need the node to be also generated that many times like for eg:

suppose the XML payload length is 1020 which it has to be splitted into 4 lines in target field.

My target structure is <Line> 0.... Unbounded and in that <Text> is 0 to 1.

So the expected result is some thing like <Line>

                                                                 <Text>

                                                            <Line>

                                                                  <Text>

                                                            <Line>

                                                                   <Text>

                                                             <Line>

                                                                    <Text>

By udf i am getting only one  which is   <Line>

                                                               <Text>

Thanks,

iaki_vila
Active Contributor
0 Kudos

Hi Chandra,

Check Luis Ortiz UDF in this thread http://scn.sap.com/thread/2056505

Regards.