写入文件时数据会重复
Data gets duplicated when written to file
我在使用 JAXB 或 BufferedWriter 时遇到问题。当我将员工添加到员工列表然后编组列表时,我得到写入 xml 文件的员工列表,但它写入列表两次。这是我的代码。
主要Class
package schleusner.cameron;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Main {
public static void main(String[] args) throws IOException, JAXBException {
Employees employees = new Employees();
employees.setEmployees(new ArrayList<Employee>());
Employee e1 = new Employee();
e1.setName("codippa");
e1.setLogin("4:30Pm");
Employee e2 = new Employee();
e2.setName("emp1");
e2.setLogin("5:00PM");
Employee e3 = new Employee();
e3.setName("emp2");
e3.setLogin("8:00AM");
Employee e4 = new Employee();
e4.setName("emp3");
e4.setLogin("1:20AM");
employees.getEmployees().add(e1);
employees.getEmployees().add(e2);
employees.getEmployees().add(e3);
employees.getEmployees().add(e4);
BufferedWriter writer1 = new BufferedWriter(new FileWriter("C:\Users\Cameron\Desktop\xmltest\Employees.xml"));
JAXBContext context = JAXBContext.newInstance(Employees.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(employees, writer1);
writer1.close();
}
}
员工人数class
package schleusner.cameron;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Employees")
@XmlAccessorType (XmlAccessType.FIELD)
public class Employees {
private List<Employee> employee;
@XmlElement(name="Employee")
public List<Employee> getEmployees() {
return employee;
}
public void setEmployees(List<Employee> employeeList) {
this.employee = employeeList;
}
}
员工Class
package schleusner.cameron;
import javax.xml.bind.annotation.XmlElement;
public class Employee {
private String name;
private String password;
private String login;
private String logout;
private String lunchin;
private String lunchout;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@XmlElement
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
@XmlElement
public String getLogout() {
return logout;
}
public void setLogout(String logout) {
this.logout = logout;
}
@XmlElement
public String getLunchin() {
return lunchin;
}
public void setLunchin(String lunchin) {
this.lunchin = lunchin;
}
@XmlElement
public String getLunchout() {
return lunchout;
}
public void setLunchout(String lunchout) {
this.lunchout = lunchout;
}
}
将 @XmlAccessorType(XmlAccessType.FIELD)
更改为 @XmlAccessorType (XmlAccessType.PROPERTY)
Employees
class。
或者将 @XmlElement(name = "Employee")
移动到 private List<Employee> employee;
。
问题是,JAXB 考虑了 @XmlAccessorType
以及显式注释。所以基本上你有两个属性 employee
和 employees
,都映射到 Employee
元素。这会导致重复输出。
我在使用 JAXB 或 BufferedWriter 时遇到问题。当我将员工添加到员工列表然后编组列表时,我得到写入 xml 文件的员工列表,但它写入列表两次。这是我的代码。
主要Class
package schleusner.cameron;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Main {
public static void main(String[] args) throws IOException, JAXBException {
Employees employees = new Employees();
employees.setEmployees(new ArrayList<Employee>());
Employee e1 = new Employee();
e1.setName("codippa");
e1.setLogin("4:30Pm");
Employee e2 = new Employee();
e2.setName("emp1");
e2.setLogin("5:00PM");
Employee e3 = new Employee();
e3.setName("emp2");
e3.setLogin("8:00AM");
Employee e4 = new Employee();
e4.setName("emp3");
e4.setLogin("1:20AM");
employees.getEmployees().add(e1);
employees.getEmployees().add(e2);
employees.getEmployees().add(e3);
employees.getEmployees().add(e4);
BufferedWriter writer1 = new BufferedWriter(new FileWriter("C:\Users\Cameron\Desktop\xmltest\Employees.xml"));
JAXBContext context = JAXBContext.newInstance(Employees.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(employees, writer1);
writer1.close();
}
}
员工人数class
package schleusner.cameron;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Employees")
@XmlAccessorType (XmlAccessType.FIELD)
public class Employees {
private List<Employee> employee;
@XmlElement(name="Employee")
public List<Employee> getEmployees() {
return employee;
}
public void setEmployees(List<Employee> employeeList) {
this.employee = employeeList;
}
}
员工Class
package schleusner.cameron;
import javax.xml.bind.annotation.XmlElement;
public class Employee {
private String name;
private String password;
private String login;
private String logout;
private String lunchin;
private String lunchout;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@XmlElement
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
@XmlElement
public String getLogout() {
return logout;
}
public void setLogout(String logout) {
this.logout = logout;
}
@XmlElement
public String getLunchin() {
return lunchin;
}
public void setLunchin(String lunchin) {
this.lunchin = lunchin;
}
@XmlElement
public String getLunchout() {
return lunchout;
}
public void setLunchout(String lunchout) {
this.lunchout = lunchout;
}
}
将 @XmlAccessorType(XmlAccessType.FIELD)
更改为 @XmlAccessorType (XmlAccessType.PROPERTY)
Employees
class。
或者将 @XmlElement(name = "Employee")
移动到 private List<Employee> employee;
。
问题是,JAXB 考虑了 @XmlAccessorType
以及显式注释。所以基本上你有两个属性 employee
和 employees
,都映射到 Employee
元素。这会导致重复输出。