Can somebody help me to create Java Object from this below XML file using Digester api.
I have a class Company has a map like this
Map<String,Department> nameToDeptMap= new HashMap<String,Department>();
I want to load below XML data to this map.
Department is super class. Development,Testing,Requirment...etc are extending that class.
<Company>
<Employee empname="xyz" department ="com.compnayname.departments.department.Development>
<Employee empname="xyza" department ="com.compnayname.departments.department.Testing>
<Employee empname="xyzab" department ="com.compnayname.departments.department.Requiremetns>
<Employee empname="xyzabc" department ="com.compnayname.departments.department.Production>
.
.
.
.
</Company>
when the Map is loaded, I will pass the "empname" to get department Object.
If you want to know how I am doing please see below code. I know it is wrong.
My java files are like this..
I am able to write Digester rules to get the values from XML file as String but don't know how to get as a Object.
Digester digester = new Digester();
digester.addObjectCreate("Company/Employee", Company.class);
digester.addCallMethod("Company/Employee", "setComapnyConfigMap", 2);
digester.addCallParam("Company/Employee", 0, "empname");
digester.addCallParam("Company/Employee", 1, "department");
:> Using JDK 1.6 , commons-digester-2.0.
This is NOT what Digester is intended to do:
"Many projects read XML configuration files to provide initialization of various Java objects within the system. There are several ways of doing this, and the Digester component was designed to provide a common implementation that can be used in many different projects."
What you are trying to do is much more easily accomplished using something like XStream or even DOM parsing (jdom or dom4j).