我们可以从xml创建一个java对象,而不知道它属于哪个对象

时间:2013-03-26 17:58:50

标签: java xml-serialization

我有一个xml内容,我不知道它属于哪个类。我需要使用XML提供的信息创建一个动态java对象。有可能这样做吗?它可能是简单的java对象,然后我们可以使用java反射来获取该对象的值。例如,

<Employee>
  <name>Jack</name>
  <designation>Manager</designation>
  <department>Finance</department>
</Employee>

所以,从这个xml,我需要转换为Employee对象。但是,我的classpath中没有Employee类。是否可以使用提供的XML创建对象?

3 个答案:

答案 0 :(得分:0)

File fXmlFile = new File("Employee.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    //optional, but recommended
    //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("Employee");

    System.out.println("----------------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement = (Element) nNode;

            System.out.println("Name        :- " + eElement.getElementsByTagName("name").item(0).getTextContent());
            System.out.println("Designation :-" + eElement.getElementsByTagName("designation").item(0).getTextContent());
            System.out.println("Department  :- " + eElement.getElementsByTagName("department").item(0).getTextContent());


        }
    }

答案 1 :(得分:0)

您可以使用JCodemodel动态生成Java类。 JCodeModel在1.6 + jdk中附带了JAX-B,并且还作为在http://codemodel.java.net/托管的单独项目分离出来。

对于代码生成,您需要查找需要使用解析API的DOM / SAX / Stax API的XML节点类型。但是,只有XML可用,才能管理每个Java属性的数据类型。如果您只使用String属性,那么可以探索此路径。

答案 2 :(得分:-1)

您可以解析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的类生成执行选择try块。

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;
}