在将 XML 映射到 Java Bean 方面需要帮助
Need help in mapping XML to a Java Bean
我有一个 XML 如下 ...
<employeeId>323</employeeId>
<name>Samuel DCosta</name>
<department>
<departmentId>2</departmentId>
<name>Accounts</name>
</department>
<salary>11290</salary>
我想将这些值映射到我拥有的 Java Beans .... 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;
}
}
您可以使用JAX-B
Java Architecture for XML Binding (JAXB) provides a fast and
convenient way to bind XML schemas and Java representations, making it
easy for Java developers to incorporate XML data and processing
functions in Java applications. As part of this process, JAXB provides
methods for unmarshalling (reading) XML instance documents into Java
content trees, and then marshalling (writing) Java content trees back
into XML instance documents. JAXB also provides a way to generate XML
schema from Java objects
首先,将 JAXB 注释添加到您的 bean class:
@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);
}
}
}
您可以为您提供的 xml 生成一个 xsd 文件,然后使用 JAXB 自动生成 java 类 对应于使用 eclipse 的模式。
要自动生成 java 类,您可以借助以下 link:
How can I get the "Eclipse >Generate>Jaxb classes" option back?
我在我的测试代码中使用了你原来的 XML 和 2 个 Java Beans- 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
告诉我结果!!! :)
我有一个 XML 如下 ...
<employeeId>323</employeeId>
<name>Samuel DCosta</name>
<department>
<departmentId>2</departmentId>
<name>Accounts</name>
</department>
<salary>11290</salary>
我想将这些值映射到我拥有的 Java Beans .... 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;
}
}
您可以使用JAX-B
Java Architecture for XML Binding (JAXB) provides a fast and convenient way to bind XML schemas and Java representations, making it easy for Java developers to incorporate XML data and processing functions in Java applications. As part of this process, JAXB provides methods for unmarshalling (reading) XML instance documents into Java content trees, and then marshalling (writing) Java content trees back into XML instance documents. JAXB also provides a way to generate XML schema from Java objects
首先,将 JAXB 注释添加到您的 bean class:
@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);
}
}
}
您可以为您提供的 xml 生成一个 xsd 文件,然后使用 JAXB 自动生成 java 类 对应于使用 eclipse 的模式。 要自动生成 java 类,您可以借助以下 link: How can I get the "Eclipse >Generate>Jaxb classes" option back?
我在我的测试代码中使用了你原来的 XML 和 2 个 Java Beans- 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
告诉我结果!!! :)