【发布时间】:2016-08-23 07:21:59
【问题描述】:
我有以下代码来创建 XML 并通读它, 我想要实现的是,根据其 ID 删除特定节点。
classemployee.java -
package com.howtodoinjava.jaxb.examples.list;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "employee")
@XmlAccessorType (XmlAccessType.FIELD)
public class Employee
{
private Integer id;
private String firstName;
private String lastName;
private double income;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
}
ClassEmployees.java -
package com.howtodoinjava.jaxb.examples.list;
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
{
@XmlElement(name="employee")
private List<Employee> employees = null;
private Integer size;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}
类 TestEmployeeMarshing.java -
package com.howtodoinjava.jaxb.examples.list;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class TestEmployeeMarshing
{
static Employees employees = new Employees();
static
{
employees.setEmployees(new ArrayList<Employee>());
Employee emp1 = new Employee();
emp1.setId(1);
emp1.setFirstName("John");
emp1.setLastName("Doe");
emp1.setIncome(10000.0);
Employee emp2 = new Employee();
emp2.setId(2);
emp2.setFirstName("Jane");
emp2.setLastName("Doe");
emp2.setIncome(20000.0);
Employee emp3 = new Employee();
emp3.setId(3);
emp3.setFirstName("Bacon");
emp3.setLastName("Butter");
emp3.setIncome(30000.0);
Employee emp4 = new Employee();
emp4.setId(4);
emp4.setFirstName("Pepparoni");
emp4.setLastName("Pizza");
emp4.setIncome(40000.0);
employees.getEmployees().add(emp1);
employees.getEmployees().add(emp2);
employees.getEmployees().add(emp3);
employees.getEmployees().add(emp4);
}
public static void main(String[] args) throws JAXBException
{
marshalingExample();
System.out.println("************************************************");
unMarshalingExample();
}
private static void unMarshalingExample() throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
employees = new Employees();
employees = (Employees) jaxbUnmarshaller.unmarshal( new File("G:/xml/employees.xml") );
List<Employee> tempEmp= employees.getEmployees();
for (int i = 0; i < tempEmp.size(); i++) {
if(tempEmp.get(i).getId().equals(3)){
System.out.println("ID equals 3");
tempEmp.remove(i);
}
}
marshalingExample();
}
private static void marshalingExample() throws JAXBException
{
JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(employees, System.out);
jaxbMarshaller.marshal(employees, new File("G:/xml/employees.xml"));
}
}
我需要删除 ID 为 1 的节点并添加一个新节点。
【问题讨论】:
-
所以?是什么阻止你这样做?从列表中删除 id=1 的项目,添加替换元素并将列表设置回员工。完成。
-
顺便说一句:您从未将 fxml 的内容读取到您的
static employees字段中,而是将其读取到局部变量中。这样,您永远不会修改写入输出文件的对象。此外,如果多个Employees 可以具有相同的id,则此代码不起作用,因为它将跳过两个相邻Employees 中的第二个id为3。如果ids 是唯一的,break;语句应插入到if正文中。
标签: java xml jaxb marshalling unmarshalling