简体   繁体   English

使用Digester从XML文件创建Java对象

[英]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. 有人可以帮助我使用Digester API从下面的XML文件中创建Java对象。

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. 我想将下面的XML数据加载到此地图。

     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. 加载地图时,我将传递“ empname”以获取部门对象。

If you want to know how I am doing please see below code. 如果您想知道我的状况,请参见下面的代码。 I know it is wrong. 我知道这是错的。

My java files are like this.. 我的Java文件就是这样。

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规则,以XML文件的形式从String中获取值,但不知道如何以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: 这不是Digester要做的:

"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." “许多项目读取XML配置文件以提供系统中各种Java对象的初始化。有多种方法可以这样做,而Digester组件旨在提供可在许多不同项目中使用的通用实现。”

What you are trying to do is much more easily accomplished using something like XStream or even DOM parsing (jdom or dom4j). 使用XStream甚至是DOM解析(jdom或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? 我还不太清楚您要在此处实现的目标:您是否需要为每个员工创建一个新的Department实例,还是要将员工ID映射到现有部门?

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. 无论哪种方式,使用Digester都可以执行许多操作,但是,如果您不熟悉Digester,最简单的方法就是只更新setComapnyConfigMap方法以接受字符串参数,然后在其中进行处理。 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: 那么您的Digester代码就是您已经拥有的代码:

  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. 如果要将员工映射到特定部门,请使摘要代码相同,而只需更改Company.setComapnyConfigMap方法的内容。

(I'm using JDK7 / Digester 3, but it should be fine in JDK6 / Digester 2) (我使用的是JDK7 / Digester 3,但在JDK6 / Digester 2中应该没问题)

Cheers, 干杯,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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