Posted on July 25, 2014 at 10.16 AM - Kuala Lumpur, Malaysia
Sometimes there is requirement where fields from a source node is to be populated as an array at the target XML. Let consider this example below.
Source XML
<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_Fields xmlns:ns0="erpf:CA:Testing1">
<Name>Testing</Name>
<Age>10</Age>
</ns0:MT_Fields>
Target XML
<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_FieldCollections xmlns:ns0="erpf:CA:Testing1">
<Field>
<FieldName>Name</FieldName>
<FieldValue>Testing</FieldValue>
</Field>
<Field>
<FieldName>Age</FieldName>
<FieldValue>10</FieldValue>
</Field>
</ns0:MT_FieldCollections>
So how do we achieve this requirement?
Following is the UDF to create target nodes as many as the source fields (which is hard coded to 2 in this example) :
for (int i=0; i<input.length; i++) {
for (int j=0; j<2; j++) {
result.addValue(" ");
}
}
An this is how the UDF is being used
Following is the UDF to populate the field name to the target field:
for (int i=0; i<input.length; i++) {
String[] output = input[i].split(" ");
for (int j=0; j<output.length; j++) {
result.addValue(output[j]);
result.addContextChange();
}
}
And this how the UDF is being used. (The name is hard coded in the constants, but with a little bit effort, we should be able to get the field names from the source actually).
The same UDF can be used to populate the field value to the target field, the only difference is that we have to concatenate all the input field values into a string first prior to be fed to the UDF.
Hope it helps.
Life is beautiful! Let's make it meaningful and colorful!