on 07-25-2007 4:45 AM
All,
we are getting a response from a system in the following structure,
<response>
<responsetext><Employee><name>john</name></Employee><Employee><name>sam</name></Employee></responsetext>
</response>
The entire XML between <responsetext> is a string value of that node. we need to get the target xml of the following structure,
<target>
<Employee>
<name>john</name>
</Employee>
<Employee>
<name>sam</name>
</Employee>
</target>
Can we do this using UDF or Advanced UDF?
Thanks for the help.
Hi,
Here i am sending UDF code to keep all <b>name</b> element values(i.e john and sam) in output array.
Input to this UDF is just <b>responsetext</b> element from source message and output you will get as
<b>john</b>
<b>sam</b>
if you want to assign this output to target <b>name</b> element for each <b>Employee</b> node occurrence,you need to use <b>splitbyValue</b> node function.
Here is the code( <b>Context</b> UDF):
StringBuffer sb = new StringBuffer(a[0]); //a is input to this UDF
int start = 0;
while(sb.indexOf("<name>",start) > 0)
{
int begin = sb.indexOf("<name>",start);
int end = sb.indexOf("</name>",start + "</name>".length());
result.addValue(sb.substring(begin + "<name>".length(),end));
start = sb.indexOf("</name>",end) ;
}
Please let me know if there are any issues with this code
Cheers,
Jag
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the replies. Probably i should have mentioned this before.
The source structure apart from <name> also might have some other optional nodes like <alias>, <title>.
<response>
<responsetext><Employee><name>john</name><alias>JJ</alias></Employee><Employee><name>sam</name><title>Manager</title></Employee></responsetext>
</response>
And the target structure needs to have the xml nodes formed also. like as below,
<target>
<Employee>
<name>john</name>
<alias>JJ</alias>
</Employee>
<Employee>
<name>sam</name>
<title>Manager</title>
</Employee>
</target>
how can we create the nodes on the target side... getting the xml and assigining it to the target side will again create a node with text value (xml text).
Thanks.
Used Jagadish's idea and built a couple of Advanced UDFs to achieve the result.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
If your source message has multiple elements like <alias>,<title> and etc..
you can use same UDF code with minor changes as follows.
1. Instead of passing one Input(<b>responsetext</b>),pass 2 inputs
i.e the 2nd input is constant tag name(if you are mapping to <b>alias</b> target element pass <b><alias></b> as 2nd input)
2. Modified code as follows
StringBuffer sb = new StringBuffer(a[0]); //a is input to this UDF
//b is 2nd input i.e constant tagname
int start = 0;
int begin = 0;
int end = 0;
while(sb.indexOf(b[0],start) > 0)
{
begin = sb.indexOf(b[0],start);
end = sb.indexOf(b[0],start + b[0].length());
result.addValue(sb.substring(begin + b[0].length(),end));
start = sb.indexOf(b[0],end) ;
}
Please let me know if you have any further issues with my UDF code
Cheers,
Jag
Since the <Employee> tag in the input can occur many times as a part of <responsetext> tag, it would be best to loop it. Give responsetext as input to your UDf and do this
<responsetext> = a(Argument passed to UDF)
while(a.indexOf("</Employee>") != -1)
{
String b = a.substring(0,a.indexOf("</Employee>"));
String name = b.substring(b.indexOf("<Employee><name>"),b.indexOf("</Employee></name>"));
resultlist.add(name);
a = a.substring(a.indexOf("</Employee>"),a.length());
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
You can do this using UDF. You need to send input, String <responsetext> (e.g. String InputStringName = Responsetext) and use substring function as
Output = InputStringName.substring(starting position,ending position) .The Output can be added to resultlist.
Regards,
Rahul
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
yes you can do it using a UDF. From the structure it looks like the Employee is a repeatable structure. hence you can take all the values of a node above employees into a ResultList (you can select the argument as a queue) and then add the entries of Employees to it. Convert it to a string and then finally return the string. map it to the target structure.
reward if helpful.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.