【问题标题】:Need help in mapping XML to a Java Bean在将 XML 映射到 Java Bean 方面需要帮助
【发布时间】:2015-04-29 09:03:37
【问题描述】:

我有一个如下的 XML ...

<employeeId>323</employeeId>
<name>Samuel DCosta</name>
<department>
    <departmentId>2</departmentId>
    <name>Accounts</name>
</department>
<salary>11290</salary>

我想将这些值映射到我拥有的 Java Bean .... XML 中的键与 bean 中成员的名称匹配 ... 有人告诉我是否有简单的方法请在 Java 中执行此操作 .... 欢迎使用工具或组件 ...

部门....

import java.io.Serializable;

public class Department implements Serializable
{
private Long departmentId;

private String name;

@Override
public String toString()
{
    return "Department [departmentId=" + departmentId + ", name=" + name + "]";
}

public Long getDepartmentId()
{
    return departmentId;
}

public void setDepartmentId(Long departmentId)
{
    this.departmentId = departmentId;
}

public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name = name;
}
}

员工.....

import java.io.Serializable;

public class Employee implements Serializable
{
private Long employeeId;

private String name;

private Department department;

private Integer salary;

@Override
public String toString()
{
    return "Employee [employeeId=" + employeeId + ", name=" + name + ", department=" + department + ", salary="
            + salary + "]";
}

public Long getEmployeeId()
{
    return employeeId;
}

public void setEmployeeId(Long employeeId)
{
    this.employeeId = employeeId;
}

public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name = name;
}

public Department getDepartment()
{
    return department;
}

public void setDepartment(Department department)
{
    this.department = department;
}

public Integer getSalary()
{
    return salary;
}

public void setSalary(Integer salary)
{
    this.salary = salary;
}
}

【问题讨论】:

标签: java xml javabeans


【解决方案1】:

您可以使用JAX-B

用于 XML 绑定的 Java 架构 (JAXB) 提供了一种快速且 绑定 XML 模式和 Java 表示的便捷方式,使其 Java 开发人员易于合并 XML 数据和处理 Java 应用程序中的函数。作为此过程的一部分,JAXB 提供 将 XML 实例文档解组(读取)成 Java 的方法 内容树,然后编组(写入)Java 内容树 到 XML 实例文档中。 JAXB 还提供了一种生成 XML 的方法 Java 对象的架构

【讨论】:

    【解决方案2】:

    首先,将 JAXB 注解添加到您的 bean 类中:

        @XmlRootElement
        public class Employee {
        private Long employeeId;
        private String name;
        private Department department;
    
        private Integer salary;
        //getter and setter omitted here
       }
    
    @XmlRootElement
    public class Department {
        private Long departmentId;
        private String name;
         //getter and setter omitted here
    }
    

    这是我用于测试的“employee.xml”文件:

    <employee>
        <employeeId>323</employeeId>
        <name>Samuel DCosta</name>
        <department>
            <departmentId>2</departmentId>
            <name>Accounts</name>
        </department>
        <salary>11290</salary>
    </employee>
    

    然后就可以像这样读取XML文件了

    public class EmployeeReader {
    public static <T> T fromXML(Reader reader,Class<T> type) throws JAXBException {
        JAXBContext context=JAXBContext.newInstance(type);
        Unmarshaller unmarshaller=context.createUnmarshaller();
        return (T)unmarshaller.unmarshal(reader);
    }
    
        public static void main(String[] args)
        {
            Reader reader=null;
            try
            {
                reader=new FileReader("employee.xml");
                Employee employee= EmployeeReader.fromXML(reader,Employee.class);
                System.out.println(employee.getName());
                System.out.println(employee.getDepartment().getName());
            }catch (Exception e)
            {
                e.printStackTrace();
            }finally {
            IOUtils.closeQuietly(reader);
        }
        }
    }
    

    【讨论】:

    • fromXML 方法将只返回 Employee bean。不能将其概括为适用于任何类型的 Java Bean 吗?
    • @Brian James,是的,我们可以在这里使用泛型
    【解决方案3】:

    我在测试代码中使用了您的原始 XML 和 2 个 Java Bean——Employee 和 Department

    import java.util.ArrayList;
    import java.util.List;
    
    import cjm.component.cb.object.ToObject;
    
    public class EmployeeConversion
    {
    public static void main(String[] args)
    {
        try
        {
            String xml = "<employeeId>323</employeeId><name>Samuel DCosta</name><department><departmentId>2</departmentId><name>Accounts</name></department><salary>11290</salary>";
    
            List<Object> objectList = new ArrayList<Object>();
    
            objectList.add(new Employee());
            objectList.add(new Department());         // adding all the nested objects within the Employee bean into this list
    
            Employee employee = (Employee) new ToObject().convertToObject(xml, new Employee(), objectList); // this will carry out the mapping to the bean
    
            System.out.println(employee);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    }
    

    输出

    -------- XML Detected -------- 
    -------- XML Detected -------- 
    -------- Map created Successfully -------- 
    -------- Object created Successfully -------- 
    Employee [employeeId=323, name=Samuel DCosta, department=Department [departmentId=2, name=Accounts], salary=11290]
    

    您将需要转换框(适用于 v1.1) http://capsulesforthejavamind.blogspot.in/2015/01/conversion-box.html

    告诉我结果!!! :)

    【讨论】:

      【解决方案4】:

      您可以为您提供的 xml 生成一个 xsd 文件,然后使用 JAXB 使用 eclipse 自动生成与架构对应的 java 类。 对于自动生成 java 类,您可以参考以下链接: How can I get the "Eclipse >Generate>Jaxb classes" option back?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-04
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 2010-12-21
        • 1970-01-01
        • 1970-01-01
        • 2010-12-27
        相关资源
        最近更新 更多