Hi, Is there any alternative way to recursion? The classs i am dealing are as follows TibrvMsg This is message class which contains fields of type TirvMsgField TibrvMsg can also contain TirvMsgField of type TibrvMsg.That means message can contain fields which are message themselves. I can use recursion to print all the fields.But i want to modify fields and add the to another Message. I am wondering if there is any alternative way to recursion?
import com.tibco.tibrv.*;
public class ShowMsg {
static TibrvMsg modMsg =new TibrvMsg();
static int id = 0;
public static void main(String[] args) throws TibrvException{
TibrvMsg msg = getMsg();
TibrvMsg modMsg = getModMsg(msg);
//System.out.println(modMsg);
for(int i=0;i<modMsg.getNumFields();i++){
TibrvMsgField field = modMsg.getFieldByIndex(i);
System.out.println(field.name+"-------"+field.id);
}
}
public static TibrvMsg getMsg(){
TibrvMsg msg = new TibrvMsg();
try{
TibrvMsg subMsg = new TibrvMsg();
subMsg.add("S1","43333");
subMsg.add("S2","7377773");
subMsg.add("S3","8388883");
//subMsg.add("SUBSUB", subSubMsg);
msg.add("Field1", "JJSJJS");
msg.add("Field2", "JDSKJKS");
msg.add("Field3", "9299399");
msg.add("Field4", "HHJJSJJSJ");
msg.add("SUB",subMsg);
}
catch(TibrvException rv){
System.out.println(rv);
}
return msg;
}
public static TibrvMsg getModMsg(TibrvMsg msg){
try{
int total = msg.getNumFields();
for(int i=0;i<total;i++){
TibrvMsgField field = msg.getFieldByIndex(i);
if(field.type==TibrvMsg.MSG){
getModMsg((TibrvMsg)field.data);
}
else{
field.id = id++;
msg.updateField(field);
}
}
}
catch(TibrvException rv){
System.out.println(rv);
}
return msg;
}
} The method getMsg() returns sample message.In getModMsg() i m using recursion and it works, that means i am able to print each and every field and subfield. Now in this method, i want to modify the field properties and update the message.Means method should return the modified message.Hence i m using :
field.id = id++; msg.updateField(field); This is not working.What i want now is to create a modified message using above functions.