JAXB hello worldの例
この記事で使用されているテクノロジ
-
JDK 1.6
-
JAXB 2.0
JAXBを使って作業するのは簡単ですが、JAXBアノテーションを使用してオブジェクトに注釈を付けるだけで、後で `jaxbMarshaller.marshal()`または `jaxbMarshaller.unmarshal()`を使用してオブジェクト/XML変換を行います。
1. JAXB依存関係
JDK1.6以上を使用している場合は、JDK 1.6にhttp://jaxb.java.net/guide/Which
JAXB
RI
is
included
in
which
JDK
.html[JAXBがバンドルされているため、追加のjaxbライブラリは必要ありません。
注意JDK <1.6では、JAXBをhttp://jaxb.java.net/[here]からダウンロードし、プロジェクトのクラスパスに「
jaxb-api.jar
」と「
jaxb-impl.jar
」を挿入します。
2. JAXBアノテーション
XMLファイルとの変換が必要なオブジェクトの場合、JAXBアノテーションで注釈を付ける必要があります。このアノテーションは自明ではありません。詳細については、このhttp://jaxb.java.net/tutorial/[JAXBガイド]を参照してください。
package com.mkyong.core; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Customer { String name; int age; int id; public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } public int getAge() { return age; } @XmlElement public void setAge(int age) { this.age = age; } public int getId() { return id; } @XmlAttribute public void setId(int id) { this.id = id; } }
3.オブジェクトをXMLに変換する
JAXBマーシャリングの例では、顧客オブジェクトをXMLファイルに変換します。 `jaxbMarshaller.marshal()`にはたくさんのオーバーロードされたメソッドがあり、出力に合ったものを見つけます。
package com.mkyong.core; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class JAXBExample { public static void main(String[]args) { Customer customer = new Customer(); customer.setId(100); customer.setName("mkyong"); customer.setAge(29); try { File file = new File("C:\\file.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); //output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB__FORMATTED__OUTPUT, true); jaxbMarshaller.marshal(customer, file); jaxbMarshaller.marshal(customer, System.out); } catch (JAXBException e) { e.printStackTrace(); } } }
出力
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <customer id="100"> <age>29</age> <name>mkyong</name> </customer>
4. XMLをオブジェクトに変換する
JAXBアンマーシャリングの例では、XMLファイルのコンテンツを顧客オブジェクトに変換します。 `jaxbMarshaller.unmarshal()`には多くのオーバーロードされたメソッドが含まれています。
package com.mkyong.core; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class JAXBExample { public static void main(String[]args) { try { File file = new File("C:\\file.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file); System.out.println(customer); } catch (JAXBException e) { e.printStackTrace(); } } }
出力
Customer[name=mkyong, age=29, id=100].... === 参考文献 . http://jaxb.java.net/[JAXBオフィシャルウェブサイト] . http://download.oracle.com/javaee/5/tutorial/doc/bnazg.html[J2EE 5 JAXBチュートリアル] link://タグ/hello-world/[hello world]link://タグ/java/[java]link://tag/jaxb/[jaxb]link://tag/xml/[xml]