繁体   English   中英

JAXB从现有XML添加/删除节点

[英]JAXB add/remove node from existing XML

我有以下代码来创建XML并仔细阅读它,我想要实现的是根据ID删除特定的节点。

class employee.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;
    }
}

员工类.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的Node并添加一个新Node。

最好将使用数据读取的代码与使用数据的代码分开,例如,将代码的各个部分放入不同的方法。 此外,将数据传递给编组方法以使其更易于重用,而不是依赖于static employees字段来传递数据会更加方便。

Employees实例中的List可以像其他可修改的List一样进行修改:

private static Employees unmarshalFromFile(String fileName) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (Employees) jaxbUnmarshaller.unmarshal(new File(fileName));
}

private static void marshalToFile(Employees data, String fileName) throws JAXBException
{
    JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(data, new File(fileName));
}

public static void main(String[] args) throws JAXBException {
    Employees data = unmarshalFromFile("G:/xml/employees.xml");

    Integer removeId = 1;
    data.getEmployees().removeIf((Employee emp) -> removeId.equals(emp.getId()));

    Employee newEmployee = ...
    data.getEmployees().add(newEmployee);

    marshalToFile(data, "G:/xml/employees.xml");
}

removeIf是在Java 8中添加的,但是您也可以在早期版本中通过遍历该列表来执行此操作:

Iterator<Employee> iterator = data.getEmployees().iterator();
while (iterator.hasNext()) {
    if (removeId.equals(iterator.next().getId())) {
         iterator.remove();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM