cancel
Showing results for 
Search instead for 
Did you mean: 

Mapping doubt

Former Member
0 Kudos

Hi,

I have question on UDF code.

My source xml format is like below

<A>one</A>

<B>two</B>

<C>Three</C>

<D>four</D>   (we may/may not get 3 different contexts)

<D>five</D>

<D>Six</D>

<E>seven</E>  (we may/may not get 3 different contexts)

<E>eight</E>

<E>nine</E>

<F>ten</F>

So total 6 input fields, I need to send all of them in to one output xml field called "Result".

Below is the required output field format.

<Result>A value is: one

B value is: two

C value is: three

D value is: four

E value is: eight

F value is: ten

D value is: five

E value is: seven

F value is: NA

D value is: six

E value is: nine

F value is: NA

</Result>

I have written code like below, but it is not working as expected.

Appreciate your notes on this.

public void Test(String[] var1, String[] var2, String[] var3, String[] var4, String[] var5, String[] var6, ResultList result, Container container) throws StreamTransformationException{

String[] S1 = new String[1];

String[] S2 = new String[1];

String[] S3 = new String[1];

String[] S4 = new String[1];

String[] S5 = new String[1];

String[] S6 = new String[1];

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

{

S1[0]="A value is: "+ var1[i]+ "\n";

}

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

{

S2[0]="B value is: "+ var2[i]+ "\n";

}

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

{

S3[0]="C value is: "+ var3[i]+ "\n";

}

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

{

S4[0]=S4[0]+"D value is: "+ var4[i]+ "\n";

}

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

{

S5[0]=S5[0]+"E value is: "+ var5[i]+ "\n";

}

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

{

S6[0]=S6[0]+"F value is: "+ var6[i]+ "\n";

}

String concat=S1[0]+S2[0]+S3[0]+S4[0]+S5[0]+S6[0];

String mod=concat.replace("null","")

result.addValue(mod);

}

Thank you.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Mohan,

As per your source structure below code will achieve you requirement.please try with below code.

String output="";

  

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

  {

  output="A value is: "+ var1[i]+ "\n";

  }

  

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

  {

  output+="B value is: "+ var2[i]+ "\n";

  }

  

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

  {

  output+="C value is: "+ var3[i]+ "\n";

  }

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

  {

       output+="D value is: "+ var4[i]+ "\n";

       if(i<var5.length)

       {

            output+="E value is: "+ var5[i]+ "\n";

       }

       else

       {

            output+="E value is:NA \n";

       }

       if(i<var6.length)

       {

            output+="F value is: "+ var6[i]+ "\n";

       }

       else

       {

            output+="F value is:NA \n";

       }

  }

result.addValue(output);

Regards,

Bhavin

Former Member
0 Kudos

Hi Bhavin,

I tried this, it is not working for below source structure.

<A>one</A>

<B>two</B>

<C>Three</C>

<D>four</D>   (we may/may not get 3 different contexts)

<D>five</D>

<D>Six</D>

<E>seven</E>  (we may/may not get 3 different contexts)

<Lets say E context is missing here>

<E>nine</E>

<F>ten</F>

Expected output is:

<Result>A value is: one

B value is: two

C value is: three

D value is: four

E value is: seven

F value is: ten

D value is: five

E value is: NA

F value is: NA

D value is: six

E value is: nine

F value is: NA

</Result>

But it is giving output like below

<Result>A value is: one

B value is: two

C value is: three

D value is: four

E value is: seven

F value is: ten

D value is: five

E value is: nine

F value is: NA

D value is: six

E value is: NA

F value is: NA

</Result>

Appreciate your help on this.

former_member182412
Active Contributor
0 Kudos

Hi Mohan,

How to identify which context is missing, are you able to see null in second context when you click on display queue on E field? what is the logic to identify the second context is missing.

Regards,

Praveen.

Former Member
0 Kudos

Yes Praveen, I can see null if i display queue.

Former Member
0 Kudos

Hi Mohan,

If you have some identifier for context missing then below code will help you. In below example i have consider as"" for context missing.

for (int i = 0; i < 3; i++)

   {

  if(var4[i]!="")

        {

             output+="D value is: "+ var4[i]+ "\n";

        }

        else

        {

             output+="D value is:NA \n";

        }

  if(var5[i]!="")

    {

             output+="E value is: "+ var5[i]+ "\n";

        }

        else

        {

             output+="E value is:NA \n";

        }

  

        if(i<var6.length)

        {

             output+="F value is: "+ var6[i]+ "\n";

        }

        else

        {

             output+="F value is:NA \n";

        }

   }

Regards,
Bhavin

former_member182412
Active Contributor
0 Kudos

Hi Mohan,

You can use below code, after D field and E field use mapWithDefault and give the value as "NA" then if any context missing then you will get NA in the result. instead of using + operators you can use StringBuilder to append the strings in the UDF.


public void mapResult(String[] aField, String[] bField, String[] cField, String[] dField, String[] eField, String[] fField,

  ResultList result, Container container) throws StreamTransformationException {

  StringBuilder sb = new StringBuilder();

  sb.append("A value is:").append(aField[0]).append("\n");

  sb.append("B value is:").append(bField[0]).append("\n");

  sb.append("C value is:").append(cField[0]).append("\n").append("\n");

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

  sb.append("D value is:").append(dField[i]).append("\n");

  sb.append("E value is:").append(eField[i]).append("\n");

  String fValue = (i == 0) ? fField[0] : "NA";

  sb.append("F value is:").append(fValue).append("\n");

  sb.append("\n");

  }

  sb.setLength(sb.length() - 1);

  result.addValue(sb.toString());

  }

Regards,

Praveen.

Former Member
0 Kudos

Praveen,

Thank you for your reply.

I used the code, when I use UDF with "All values of context", it has displayed only first context result set.

So, I have changed to "All values of queue", i am getting result with all contexts but it displayed unwanted data in middle like below.

Any idea about how to delete that data?

<Result>A value is: one

B value is: two

C value is: three

D value is: four

E value is: seven

F value is: ten

D value is: __cC_

E value is: __cC_

F value is: __cC_

D value is: five

E value is: nine

F value is: NA

D value is: __cC_

E value is: __cC_

F value is: __cC_

D value is: six

E value is: NA

F value is: NA

</Result>

former_member182412
Active Contributor
0 Kudos

You just need to use remove context function after map with default before sending  it to UDF for D and and E fields

Former Member
0 Kudos

When i use remove context, "All values of context" udf type and code which is mentioned by you.

Below is the output i am getting.

<Result>A value is: one

B value is: two

C value is: three

D value is: four

E value is: seven

F value is: ten

D value is: five

E value is: nine

F value is: NA

D value is: six

E value is: NA

F value is: NA

</Result>

but expecting output is:

<Result>A value is: one

B value is: two

C value is: three

D value is: four

E value is: seven

F value is: ten

D value is: five

E value is: NA

F value is: NA

D value is: six

E value is: nine

F value is: NA

</Result>

Below is the source structure.

<A>one</A>

<B>two</B>

<C>Three</C>

<D>four</D>   (we may/may not get 3 different contexts)

<D>five</D>

<D>Six</D>

<E>seven</E>  (we may/may not get 3 different contexts)

<Lets say E context is missing here>

<E>nine</E>

<F>ten</F>

former_member182412
Active Contributor
0 Kudos

Hi Mohan,

I created the structures like below and the mapping. UDF is all values of context.

Testing result:

Regards,

Praveen.

Answers (0)