共通処理部分
JaxbUtil.java
* これが、今回、目的のコード
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.bind.JAXB;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
public class JaxbUtil {
public static <T> Object read(String filePath, Class<T> classType) {
try (InputStream xmlInputStream = new FileInputStream(filePath);) {
JAXBContext jaxbContext = JAXBContext.newInstance(classType);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return unmarshaller.unmarshal(xmlInputStream);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static void write(String fileName, Object targetValue) {
try (OutputStream xmlOutputStream = new FileOutputStream(fileName)) {
JAXB.marshal(targetValue, xmlOutputStream);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
* 呼び出し側にキャストさせないようにするための改良版
@SuppressWarnings("unchecked")
public static <T> T read(String filePath, Class<T> classType) {
try (InputStream xmlInputStream = new FileInputStream(filePath);) {
JAXBContext jaxbContext = JAXBContext.newInstance(classType);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object xmlObject = unmarshaller.unmarshal(xmlInputStream);
if (xmlObject == null || xmlObject.getClass() == classType.getClass()) {
return null;
}
return (T) xmlObject;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
サンプル1
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "people")
public class People {
private List<Person> people;
public People() {
this.people = new ArrayList<Person>();
}
public List<Person> getPeople() {
return this.people;
}
public void setPeople(List<Person> people) {
this.people = people;
}
public void addPerson(Person person) {
this.people.add(person);
}
}
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class Person {
private String name;
private int age;
@XmlElement
private List<Person> friends;
public Person() {
this.friends = new ArrayList<Person>();
}
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 List<Person> getFriends() {
return this.friends;
}
public void addFriend(Person person) {
this.friends.add(person);
}
}
public class Main {
public static void main(String[] args) {
People people = new People();
// Data1
Person person1 = new Person();
person1.setName("Ken");
person1.setAge(62);
Person friend11 = new Person();
friend11.setName("Tom");
friend11.setAge(52);
person1.addFriend(friend11);
Person friend12 = new Person();
friend12.setName("Smith");
friend12.setAge(26);
person1.addFriend(friend12);
people.addPerson(person1);
// Data2
Person person2 = new Person();
person2.setName("Coco");
person2.setAge(12);
Person friend21 = new Person();
friend21.setName("Bella");
friend21.setAge(2);
person2.addFriend(friend21);
Person friend22 = new Person();
friend22.setName("Pachi");
friend22.setAge(2);
person2.addFriend(friend22);
people.addPerson(person2);
JaxbUtil.write("./people.xml", people);
People peopleFromXml = (People) JaxbUtil.read("./people.xml",
People.class);
for (Person person : peopleFromXml.getPeople()) {
System.out.println("Person : " + person.getName() + "("
+ person.getAge() + ")");
for (Person friend : person.getFriends()) {
System.out.println(" Friend : " + friend.getName() + "("
+ friend.getAge() + ")");
}
}
}
}
出力結果
コンソール画面
Person : Ken(62)
Friend : Tom(52)
Friend : Smith(26)
Person : Coco(12)
Friend : Bella(2)
Friend : Pachi(2)
people.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<people>
<people>
<friends>
<age>52</age>
<name>Tom</name>
</friends>
<friends>
<age>26</age>
<name>Smith</name>
</friends>
<age>62</age>
<name>Ken</name>
</people>
<people>
<friends>
<age>2</age>
<name>Bella</name>
</friends>
<friends>
<age>2</age>
<name>Pachi</name>
</friends>
<age>12</age>
<name>Coco</name>
</people>
</people>
サンプル2
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Name")
public class Name {
private String first;
private String last;
@XmlAttribute(name="FirstName")
public String getFirst() {
return this.first;
}
public void setFirst(String first) {
this.first = first;
}
@XmlAttribute(name="LastName")
public String getLast() {
return this.last;
}
public void setLast(String last) {
this.last = last;
}
}
public class Main {
public static void main(String[] args) {
Name name = new Name();
name.setFirst("John");
name.setLast("Smith");
JaxbUtil.write("./name.xml", name);
Name nameFromXml = (Name) JaxbUtil.read("./name.xml",
Name.class);
System.out.println("Name : " + nameFromXml.getFirst() + " "
+ nameFromXml.getLast());
}
}
出力結果
コンソール画面
Name : John Smith
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Name LastName="Smith" FirstName="John"/>