【Java】 JAXBアノテーション ~ サンプル編 ~


■ サンプル

Person.java

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="person")
public class Person implements Serializable {
  private static final long serialVersionUID = 1L;
  
  @XmlElement(name = "name", nillable = false)
  private String name;
  @XmlElement(name = "age")
  private int age;
  @XmlElement(name = "woman")
  private boolean isWoman;
  
  public Person() {
    this(null, -1, false);
  }
  public Person(String name, int age, boolean isWoman) {
    this.name = name;
    this.age = age;
    this.isWoman = isWoman;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return this.age;
  }

  public void setAge(int age) {
    this.age = age;
  }
  public boolean isWoman() {
    return this.isWoman;
  }
  public void setWoman(boolean isWoman) {
    this.isWoman = isWoman;
  }
}

XmlSerializer.java

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class XmlSerializer {
  // 実験用
  public static void main(String[] args) {
    // Case1 : 単一
    Person person = new Person("Isabella", 3, true);
    String fileName1 = "person.xml";
    XmlSerializer.save(fileName1, person);

    Person newPerson = XmlSerializer.loadValue(fileName1, Person.class);

    // 出力
    System.out.println("Case1 : 単一");
    System.out.println(newPerson.getName() + "(" + newPerson.getAge() + ") is Woman? : " + newPerson.isWoman());
    System.out.println("");

    // Case2 : リスト
    List<Person> people = new ArrayList<>();
    Person person1 = new Person("Ken", 62, true);
    people.add(person1);

    Person person2 = new Person("Coco", 10, false);
    people.add(person2);

    String fileName = "people.xml";
    XmlSerializer.save(fileName, people);

    List<Person> newPeople = XmlSerializer.loadValues(fileName, Person.class);

    // 出力
    System.out.println("Case2 : リスト");
    for (Person newPersonInList : newPeople) {
      System.out.println(
          newPersonInList.getName() + "(" + newPersonInList.getAge() + ") is Woman? : " + newPersonInList.isWoman());
    }
    System.out.println("");

    System.out.println("Done");
  }

  // 共通化

  public static <T> void save(String fileName, T objectToSave) {
    try (XMLEncoder xMLEncoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(fileName)));) {
      xMLEncoder.writeObject(objectToSave);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }

  public static <T> T loadValue(String fileName, Class<T> clazz) {
    Object value = XmlSerializer.load(fileName);
    if (value == null || value.getClass() != clazz) {
      return null;
    }
    return clazz.cast(value);
  }

  @SuppressWarnings("unchecked")
  public static <T> List<T> loadValues(String fileName, Class<T> clazz) {
    Object value = XmlSerializer.load(fileName);
    if (value == null || !(value instanceof List<?>)) {
      Collections.emptyList();
    }
    List<Object> values = (List<Object>) value;
    if (values.isEmpty() || !values.get(0).getClass().isAssignableFrom(clazz)) {
      return Collections.emptyList();
    }

    return (List<T>) values;
  }

  private static Object load(String fileName) {
    try (XMLDecoder xMLDecoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(fileName)));) {
      return xMLDecoder.readObject();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    return null;
  }
}

出力結果

コンソール画面
Case1 : 単一
Isabella(3) is Woman? : true

Case2 : リスト
Ken(62) is Woman? : true
Coco(10) is Woman? : false

Done
person.xml
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_144" class="java.beans.XMLDecoder">
 <object class="com.sample.xml.Person">
  <void property="age">
   <int>3</int>
  </void>
  <void property="name">
   <string>Isabella</string>
  </void>
  <void property="woman">
   <boolean>true</boolean>
  </void>
 </object>
</java>
person.xml
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_144" class="java.beans.XMLDecoder">
 <object class="java.util.ArrayList">
  <void method="add">
   <object class="com.sample.xml.Person">
    <void property="age">
     <int>62</int>
    </void>
    <void property="name">
     <string>Ken</string>
    </void>
    <void property="woman">
     <boolean>true</boolean>
    </void>
   </object>
  </void>
  <void method="add">
   <object class="com.sample.xml.Person">
    <void property="age">
     <int>10</int>
    </void>
    <void property="name">
     <string>Coco</string>
    </void>
   </object>
  </void>
 </object>
</java>