2016-08-23 6 views
-1

からノードを削除し、 。JAXBは何を達成したいことは、それのIDに基づいて特定のノードを削除し、ある追加/私はXMLを作成しても、それを読むために、次のコードを持っている既存のXML

クラス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; 
    } 
} 

クラスEmployees.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のノードを削除して新しいノードを追加する必要があります。

+0

?あなたはそれをやり続けているのですか?リストからid = 1のアイテムを削除し、置換要素を追加してリストをEmployeesに戻します。完了しました。 – Fildor

+0

ところでfxmlの内容を 'static employees'フィールドに読み込まず、代わりにローカル変数に読み込みます。このようにして、出力ファイルに書き込まれるオブジェクトを変更することはありません。 id'sが一意である '場合は、3の' id'で2隣接 'Employee'sの第二をスキップするであろうから、複数の' Employee'sは、同じ 'id'を持つことができる場合はさらに、このコードは、動作しません。 、 'break;'文を 'if'本体に挿入する必要があります。 – fabian

答えて

2

あなたは最高の、例えば、それを使用してコードからデータを読み込むコードを区切りますコードの部分を別の方法に入れてさらに、簡単にデータを渡すためにstaticemployeesフィールドに頼るの代わりを再利用できるようにすることマーシャリングメソッドにデータを渡すために、より便利です。反復処理によって

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で追加されましたが、あなたはまた、以前のバージョンでこれを行うことができます:

Employeesインスタンス内

Listは、他の修正Listのように変更することができますリスト:だから

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

ID 1を含むNode全体を削除しますか?Java 7でこれを行うにはどうすればよいですか? –

+0

@AniruddhaRajeあなたの基準に一致する要素を削除するために 'Iterator'を使います(答えにコードを追加しました)。 – fabian

+0

removeIdとは何ですか? removeIdが –

関連する問題