I have an XML as follows ...
我有一个XML,如下所示……
<employeeId>323</employeeId>
<name>Samuel DCosta</name>
<department>
<departmentId>2</departmentId>
<name>Accounts</name>
</department>
<salary>11290</salary>
I want to map these values to the Java Beans that I have .... the keys in the XML match with the name of the members in the beans ..... someone tell me if there is a simple way to do this in Java please .... tools or components welcome ...
我想我将这些值映射到Java bean ....XML中的键与bean中成员的名字匹配……有人告诉我如果有一个简单的方法在Java请....工具或组件欢迎……
Department ....
部门....
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;
}
}
Employee .....
员工.....
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;
}
}
4 个解决方案
#1
0
I used your original XML and the 2 Java Beans- Employee and Department in my test code
我在测试代码中使用了您的原始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();
}
}
}
Output
输出
-------- XML Detected --------
-------- XML Detected --------
-------- Map created Successfully --------
-------- Object created Successfully --------
Employee [employeeId=323, name=Samuel DCosta, department=Department [departmentId=2, name=Accounts], salary=11290]
You will need the Conversion Box (go for v1.1) http://capsulesforthejavamind.blogspot.in/2015/01/conversion-box.html
您将需要转换框(转到v1.1) http://荚膜为javamind.blogspot.in5/01/conversion -box.html
Ping me with the results!!! :)
告诉我结果!!!:)
#2
4
You can use JAX-B
您可以使用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
XML绑定的Java体系结构(JAXB)提供了一种快速和方便的方式来绑定XML模式和Java表示,使Java开发人员能够很容易地将XML数据和处理函数集成到Java应用程序中。作为这个过程的一部分,JAXB提供了将XML实例文档解组(读取)到Java内容树中,然后将Java内容树编组(写入)回XML实例文档的方法。JAXB还提供了一种从Java对象生成XML模式的方法
#3
3
First, add JAXB annotation to your bean class:
首先,向bean类添加JAXB注释:
@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
}
This the 'employee.xml' file I used for testing:
这样的员工。我用于测试的xml文件:
<employee>
<employeeId>323</employeeId>
<name>Samuel DCosta</name>
<department>
<departmentId>2</departmentId>
<name>Accounts</name>
</department>
<salary>11290</salary>
</employee>
Then you can read a XML file like this
然后您可以读取这样的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);
}
}
}
#4
0
You can generate an xsd file for the xml you provided and then use JAXB for auto generating java classes corresponding to the schema using eclipse. For Auto generating the java classes, you can take help of below link: How can I get the "Eclipse >Generate>Jaxb classes" option back?
您可以为提供的xml生成xsd文件,然后使用JAXB自动生成与eclipse模式对应的java类。对于自动生成java类,您可以借助以下链接:如何获得“Eclipse >生成>Jaxb类”选项返回?
#1
0
I used your original XML and the 2 Java Beans- Employee and Department in my test code
我在测试代码中使用了您的原始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();
}
}
}
Output
输出
-------- XML Detected --------
-------- XML Detected --------
-------- Map created Successfully --------
-------- Object created Successfully --------
Employee [employeeId=323, name=Samuel DCosta, department=Department [departmentId=2, name=Accounts], salary=11290]
You will need the Conversion Box (go for v1.1) http://capsulesforthejavamind.blogspot.in/2015/01/conversion-box.html
您将需要转换框(转到v1.1) http://荚膜为javamind.blogspot.in5/01/conversion -box.html
Ping me with the results!!! :)
告诉我结果!!!:)
#2
4
You can use JAX-B
您可以使用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
XML绑定的Java体系结构(JAXB)提供了一种快速和方便的方式来绑定XML模式和Java表示,使Java开发人员能够很容易地将XML数据和处理函数集成到Java应用程序中。作为这个过程的一部分,JAXB提供了将XML实例文档解组(读取)到Java内容树中,然后将Java内容树编组(写入)回XML实例文档的方法。JAXB还提供了一种从Java对象生成XML模式的方法
#3
3
First, add JAXB annotation to your bean class:
首先,向bean类添加JAXB注释:
@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
}
This the 'employee.xml' file I used for testing:
这样的员工。我用于测试的xml文件:
<employee>
<employeeId>323</employeeId>
<name>Samuel DCosta</name>
<department>
<departmentId>2</departmentId>
<name>Accounts</name>
</department>
<salary>11290</salary>
</employee>
Then you can read a XML file like this
然后您可以读取这样的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);
}
}
}
#4
0
You can generate an xsd file for the xml you provided and then use JAXB for auto generating java classes corresponding to the schema using eclipse. For Auto generating the java classes, you can take help of below link: How can I get the "Eclipse >Generate>Jaxb classes" option back?
您可以为提供的xml生成xsd文件,然后使用JAXB自动生成与eclipse模式对应的java类。对于自动生成java类,您可以借助以下链接:如何获得“Eclipse >生成>Jaxb类”选项返回?