Using XML To Java in TIBCO BW
There are cases when it is preferable to use Java code instead of XSLT when doing a complex string manipulation. For example, you would like to produce a single string from the different segments of an XML. You can do this by using XML To Java activity in TIBCO. Suppose you have the following XML:
<?xml version = "1.0" encoding = "UTF-8"?>
<Employees xmlns = "http://xmlns.tibco.com/javaschema/javaschema.Employees">
<Employee>
<Age>24</Age>
<Level>2</Level>
<Name>Mark Cruz</Name>
<Role>Technology Consultant</Role>
<Skill>Java</Skill>
<Skill>TIBCO</Skill>
<Skill>Ruby</Skill>
</Employee>
<Employee>
<Age>25</Age>
<Level>1</Level>
<Name>John Cruz</Name>
<Role>Technology Consultant</Role>
<Skill>TIBCO BW</Skill>
<Skill>PHP</Skill>
<Skill>XML</Skill>
</Employee>
</Employees>
First off, you need to write a Java Bean that is equivalent to the above XML. A Java Bean is a class that implements the java.io.Serializable class and contains setter and getter methods (http://en.wikipedia.org/wiki/JavaBean). As observed, Employee is a repeating element which contains several fields. Its parent is Employees which is the root element. It is noted that Skill is also a repeating element. You can write first the class that represents this segment.
package javaschema;
public class EmployeeDetail implements java.io.Serializable {
private String Name;
private int Age;
private String Role;
private int Level;
private String[] Skill;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public String getRole() {
return Role;
}
public void setRole(String role) {
Role = role;
}
public int getLevel() {
return Level;
}
public void setLevel(int level) {
Level = level;
}
public String[] getSkill() {
return Skill;
}
public void setSkill(String[] skill) {
Skill = skill;
}
}
Then, you can write the class that represents Employees segment. TIBCO supports Arrays and Collections but not HashMaps. You may also notice that the root element always takes the class name, but its child elements take their name from the instance variables. For this exercise, we have used Arrays.
package javaschema;
public class Employees implements java.io.Serializable {
private EmployeeDetail[] Employee;
public EmployeeDetail[] getEmployee() {
return Employee;
}
public void setEmployee(EmployeeDetail[] employee) {
Employee = employee;
}
}
Next, you have to package all of these classes into a JAR file. In TIBCO Designer, create a new project. There are two ways to include this JAR in the classpath: using an AliasLibrary or modifying the classpath in the TRA file and copying the JAR file into the classpath. The easier way is using an AliasLibrary as it is also very portable. Add an AliasLibrary shared resource and create and/or add an alias that points to the location of the JAR file. Create a Process Definition and add XML To Java activity.
For instance, we can have the following code to produce a particular string output:
javaschema.Employees employees = (javaschema.Employees) emp;
javaschema.EmployeeDetail[] employeeDetailArray = employees.getEmployee();
java.util.List employeeList = java.util.Arrays.asList(employeeDetailArray);
StringBuilder employeesResult = new StringBuilder();
StringBuilder employeeResult = new StringBuilder();
employeesResult.append("Employees=[");
for (Object employeeDet : employeeList) {
javaschema.EmployeeDetail employeeDetail = (javaschema.EmployeeDetail) employeeDet;
employeeResult.append("Employee=[");
String name = employeeDetail.getName();
employeeResult.append("name='").append(name).append("'").append(",");
int age = employeeDetail.getAge();
employeeResult.append("age='").append(age).append("'").append(",");
String role = employeeDetail.getRole();
employeeResult.append("role='").append(role).append("'").append(",");
int level = employeeDetail.getLevel();
employeeResult.append("level='").append(level).append("'").append(",");
String[] skill = employeeDetail.getSkill();
StringBuilder skillResult = new StringBuilder();
for (int i = 0; i < skill.length; i++) {
skillResult.append("'").append(skill[i]).append("'");
if (i != skill.length - 1) skillResult.append(",");
}
employeeResult.append("skill=[").append(skillResult).append("]");
employeeResult.append("]");
if (employeeList.indexOf(employeeDetail) != employeeDetailArray.length - 1)
employeeResult.append(",");
}
employeesResult.append(employeeResult);
employeesResult.append("]");
setedi(employeesResult.toString());
Finally, running the process would give the following output:
Employees=[Employee=[name='Mark Cruz',age='24',role='Technology Consultant',level='2',skill=['Java','TIBCO','Ruby']],Employee=[name='John Cruz',age='25',role='Technology Consultant',level='1',skill=['TIBCO BW','PHP','XML']]]
<?xml version = "1.0" encoding = "UTF-8"?>
<Employees xmlns = "http://xmlns.tibco.com/javaschema/javaschema.Employees">
<Employee>
<Age>24</Age>
<Level>2</Level>
<Name>Mark Cruz</Name>
<Role>Technology Consultant</Role>
<Skill>Java</Skill>
<Skill>TIBCO</Skill>
<Skill>Ruby</Skill>
</Employee>
<Employee>
<Age>25</Age>
<Level>1</Level>
<Name>John Cruz</Name>
<Role>Technology Consultant</Role>
<Skill>TIBCO BW</Skill>
<Skill>PHP</Skill>
<Skill>XML</Skill>
</Employee>
</Employees>
First off, you need to write a Java Bean that is equivalent to the above XML. A Java Bean is a class that implements the java.io.Serializable class and contains setter and getter methods (http://en.wikipedia.org/wiki/JavaBean). As observed, Employee is a repeating element which contains several fields. Its parent is Employees which is the root element. It is noted that Skill is also a repeating element. You can write first the class that represents this segment.
package javaschema;
public class EmployeeDetail implements java.io.Serializable {
private String Name;
private int Age;
private String Role;
private int Level;
private String[] Skill;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public String getRole() {
return Role;
}
public void setRole(String role) {
Role = role;
}
public int getLevel() {
return Level;
}
public void setLevel(int level) {
Level = level;
}
public String[] getSkill() {
return Skill;
}
public void setSkill(String[] skill) {
Skill = skill;
}
}
Then, you can write the class that represents Employees segment. TIBCO supports Arrays and Collections but not HashMaps. You may also notice that the root element always takes the class name, but its child elements take their name from the instance variables. For this exercise, we have used Arrays.
package javaschema;
public class Employees implements java.io.Serializable {
private EmployeeDetail[] Employee;
public EmployeeDetail[] getEmployee() {
return Employee;
}
public void setEmployee(EmployeeDetail[] employee) {
Employee = employee;
}
}
Next, you have to package all of these classes into a JAR file. In TIBCO Designer, create a new project. There are two ways to include this JAR in the classpath: using an AliasLibrary or modifying the classpath in the TRA file and copying the JAR file into the classpath. The easier way is using an AliasLibrary as it is also very portable. Add an AliasLibrary shared resource and create and/or add an alias that points to the location of the JAR file. Create a Process Definition and add XML To Java activity.
Optionally, you may use a Java Schema shared resource; otherwise, you have to specify the Library and the Class. The main advantage of using a Java Schema is that you use it as a reference in the Output Editor of the Start activity.
In the Configuration tab of the XML To Java activity, you have to check Use Java Schema and specify the Java Schema shared resource.
The schema tree will then appear in the Input tab of the XML To Java activity.
You can then use the output object in your Java code. In the Configuration tab, you can specify the input parameter as type ObjectReference. You can write a code that returns a consolidated string as an output.
For instance, we can have the following code to produce a particular string output:
javaschema.Employees employees = (javaschema.Employees) emp;
javaschema.EmployeeDetail[] employeeDetailArray = employees.getEmployee();
java.util.List employeeList = java.util.Arrays.asList(employeeDetailArray);
StringBuilder employeesResult = new StringBuilder();
StringBuilder employeeResult = new StringBuilder();
employeesResult.append("Employees=[");
for (Object employeeDet : employeeList) {
javaschema.EmployeeDetail employeeDetail = (javaschema.EmployeeDetail) employeeDet;
employeeResult.append("Employee=[");
String name = employeeDetail.getName();
employeeResult.append("name='").append(name).append("'").append(",");
int age = employeeDetail.getAge();
employeeResult.append("age='").append(age).append("'").append(",");
String role = employeeDetail.getRole();
employeeResult.append("role='").append(role).append("'").append(",");
int level = employeeDetail.getLevel();
employeeResult.append("level='").append(level).append("'").append(",");
String[] skill = employeeDetail.getSkill();
StringBuilder skillResult = new StringBuilder();
for (int i = 0; i < skill.length; i++) {
skillResult.append("'").append(skill[i]).append("'");
if (i != skill.length - 1) skillResult.append(",");
}
employeeResult.append("skill=[").append(skillResult).append("]");
employeeResult.append("]");
if (employeeList.indexOf(employeeDetail) != employeeDetailArray.length - 1)
employeeResult.append(",");
}
employeesResult.append(employeeResult);
employeesResult.append("]");
setedi(employeesResult.toString());
Finally, running the process would give the following output:
Employees=[Employee=[name='Mark Cruz',age='24',role='Technology Consultant',level='2',skill=['Java','TIBCO','Ruby']],Employee=[name='John Cruz',age='25',role='Technology Consultant',level='1',skill=['TIBCO BW','PHP','XML']]]
It was so nice article.i was really satisfied by seeing this article and we are
ReplyDeletealso giving tibco online training.the tibco online training center is one of the best
online training Institute.
Thank you! Let me know how we can collaborate by exchanging ideas or insights about tibco.
ReplyDeleteTibco BW/EMS Online Training
ReplyDeletewww.21cssindia.com/courses/tibco-bw-ems-online-training-163.html
Tibco BW/EMS Training , Tibco BW/EMS Online Training , Tibco BW/EMS
Corporate Training, Best Tibco BW/EMS Training , Expert Tibco BW/EMS
Training ... contact@21cssindia.com or +919000444287
The above provided resources are very useful to explore TIBCO BW. One can gain knowledge from fundamentals through TIBCO BW Online Training
ReplyDeleteIt's really a good example..like it..
ReplyDeleteI am getting error in XML to Java activity in input as "Java Schema contains invalid fields".
ReplyDeleteBut I have followed what you done...plz let me know the issue.
hi rahul,
Deletedid you use "public" access for the fields, and primitive return types and classes supported by TIBCO?
Good information article.you shared good ideas.i get a differnt idea above the blog.
ReplyDeleteagain thanks for your article.
java training in chennai |
java training institutes in chennai |
java j2ee training institutes in velachery
ReplyDeleteJava is very good blog,it's highly professional course.Thanks for sharing
java online course
Aduan Ayam
ReplyDeleteGreat Job , your content is very useful.
ReplyDeleteTIBCO BusinessWork Container Edition Training
TIBCO BW Container Edition Training
TIBCO BWCE Training
TIBCO BWCE Online Training
Thank you for sharing wonderful information with us to get some idea about it.
ReplyDeleteMulesoft Training
Mulesoft Self Learning
sap bw on hana online training
ReplyDeletesap sd online training
osb online training
oracle scm online training
abinitio online training
spark online training
sap bw on hana training
ReplyDeletesap sd training
osb training
oracle scm training
abinitio training
spark training
This comment has been removed by the author.
ReplyDeleteAnyone know why i am getting issue below
ReplyDeleteBW-JAVA-100001 Job-27000 Error in [Processes/Test.process/Java Code]
While executing [invoke] encountered [java.lang.ClassCastException] : [javaschema.Employees cannot be cast to javaschema.Employees at Processes.Test.TestJavaCode.invoke(TestJavaCode.java:32)]