简体   繁体   中英

Using Digester to create Java Objects from XML file

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).

I'm not completely clear what you are trying to achieve here: do you need to create a new Department instance for each employee, or are you trying to map employee ids to existing departments?

Either way, there are many ways to do this with Digester, but perhaps a the simplest way if you are new to Digester would be to just update your setComapnyConfigMap method to accept string parameters, and do whaatever you require in there. For example, to create a new instance of the specific Department subclass for each employee:

public static class Company {
  private Map<String, Department> nameToDeptMap = new HashMap<>();

  public void setComapnyConfigMap(String empName, String deptClass)
      throws InstantiationException, IllegalAccessException, ClassNotFoundException {

    Department dept = (Department)Class.forName(deptClass).newInstance();
    nameToDeptMap.put(empName, dept);
  }
}

Then your Digester code is as you already have it:

  String xml = "<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.Requirements'/>"
      + "<Employee empname='xyzabc' department='com.compnayname.departments.department.Production'/>"
      + "</Company>";

  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");

  Company c = digester.parse(new StringReader(xml));

If you want to map an employee to a specific department, leave the digester code the same and just change the contents of the Company.setComapnyConfigMap method.

(I'm using JDK7 / Digester 3, but it should be fine in JDK6 / Digester 2)

Cheers,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM