以下xml是由需要由Java应用程序转换为Employee类型的对象的系统生成的。
<?xml version="1.0" encoding="utf-8"?>
<Employee xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Designation>Executive</Designation>
<Name>Praveen Kumar</Name>
<Salary>90000</Salary>
<BankAccount>10111</BankAccount>
</Employee>
如何使用XmlDecoder的readObject方法反序列化上述XML而不转换由另一个系统生成的xml文件?
答案 0 :(得分:0)
以下是处理用例的方法:
以下课程将用于此答案:
package forum26609152;
public class Employee {
private String designation;
private String name;
private int salary;
private String bankAccount;
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getBankAccount() {
return bankAccount;
}
public void setBankAccount(String bankAccount) {
this.bankAccount = bankAccount;
}
}
以下是XMLEncoder / XMLDecoder期望使用的XML。它与您问题中的XML非常不同。 XMLEncoder / XMLDecoder不适合您的用例。
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_60" class="java.beans.XMLDecoder">
<object class="forum26609152.Employee">
<void property="bankAccount">
<string>10111</string>
</void>
<void property="designation">
<string>Executive</string>
</void>
<void property="name">
<string>Praveen Kumar</string>
</void>
<void property="salary">
<int>90000</int>
</void>
</object>
</java>
JAXB是用于对象到XML绑定的标准API。自Java SE 6以来,JDK / JRE中已包含一个实现,因此您已经拥有它。
<强>员工强>
Java和XML之间的映射是通过注释配置的。下面是将Java对象映射到您的问题中的XML所需的注释。
import javax.xml.bind.annotation.*;
@XmlRootElement(name="Employee")
@XmlType(propOrder = {"designation", "name", "salary", "bankAccount"})
public class Employee {
private String designation;
private String name;
private int salary;
private String bankAccount;
@XmlElement(name="Designation")
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
@XmlElement(name="Name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name="Salary")
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@XmlElement(name="BankAccount")
public String getBankAccount() {
return bankAccount;
}
public void setBankAccount(String bankAccount) {
this.bankAccount = bankAccount;
}
}
演示代码
下面是您需要编写的代码,以便将XML读入对象形式,然后将其输出回XML。
import javax.xml.bind.*;
import java.io.File;
public class JaxbDemo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Employee.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("input.xml");
Employee employee = (Employee) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(employee, System.out);
}
}
<强> input.xml中/输出强>
正如您所看到的,这是您问题中的XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Employee>
<Designation>Executive</Designation>
<Name>Praveen Kumar</Name>
<Salary>90000</Salary>
<BankAccount>10111</BankAccount>
</Employee>