您可以解析 XML 并生成对象的源代码。
使用您的示例,生成的类将是:
public class Employee {
private String name;
private String designation;
private String department;
public Employee(String name, String designation, String department) {
this.name = name;
this.designation = designation;
this.department = department;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
您使用大量 StringBuilder 方法生成这样的代码。这是我的 Java 项目中的一种方法。此方法为针对数据库执行 SQL 的类生成执行选择尝试块。
protected static final String DELIM_LINE = System
.getProperty("line.separator");
protected StringBuilder generateExecuteSelectTryBlock(String ps,
StringBuilder variables) {
StringBuilder sb = new StringBuilder();
sb.append("\t\ttry {");
sb.append(DELIM_LINE);
sb.append("\t\t\tprepare");
sb.append(ps);
sb.append("Select(");
if (variables != null) {
sb.append(variables);
}
sb.append(");");
sb.append(DELIM_LINE);
sb.append("\t\t\tResultSet rs = ps");
sb.append(ps);
sb.append(".executeQuery();");
sb.append(DELIM_LINE);
sb.append("\t\t\treturn rs;");
sb.append(DELIM_LINE);
sb.append("\t\t} catch (SQLException e) {");
sb.append(DELIM_LINE);
sb.append("\t\t\tif (e.getErrorCode() == +100) return null;");
sb.append(DELIM_LINE);
sb.append("\t\t\tDB2Connection.sqlException(e);");
sb.append(DELIM_LINE);
sb.append("\t\t}");
sb.append(DELIM_LINE);
sb.append("\t\treturn null;");
sb.append(DELIM_LINE);
return sb;
}