¢¨ Java 5 °Ê¹ß¤Ê¤é¤Ð JAXB2 ¤ÎÊý¤¬¤ª¤¹¤¹¤á
C:\Sun\jwsdp-1.5\jaxb\lib\*.lib C:\Sun\jwsdp-1.5\jaxp\lib\*.lib C:\Sun\jwsdp-1.5\jaxp\lib\endorsed\*.lib C:\Sun\jwsdp-1.5\jwsdp-shared\lib\*.lib¤¬É¬Íפʤ褦¤Ç¤¹¡£¤½¤ì¤é¤ò³°ÉôJAR¤È¤·¤Æ¥×¥í¥¸¥§¥¯¥È¤Ë¥¤¥ó¥Ý¡¼¥È¤·¤Þ¤¹
ËÜ¥µ¥¤¥È¤ÎXMLSchema¤Ë´Ø¤¹¤ë¥á¥â?¤Çºî¤Ã¤¿XMLSchema¤ò¿¾¯Ê£»¨¤Ë¤·¤ÆÁàºî¤·¤Þ¤¹¡£
yamada.xml
<?xml version="1.0" encoding="UTF-8"?>
<employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="./example.xsd">
  <name>YAMADA Taro</name>
  <id>1</id>
  <hire>2001-01-01+09:00</hire>
  <telephone>
    <cell>090-1234-5678</cell>
    <office>03-9876-5432</office>
  </telephone>
  <facialPortrait>0102030405060708090A0B0C0D0E0F</facialPortrait>
  <subordinate>SATO Hoshi</subordinate>
  <subordinate>James Tiberius Kirk</subordinate>
</employee>
example.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--W3C Schema generated by XMLSpy v2005 sp1 U (http://www.xmlspy.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="employee">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="id" type="xs:positiveInteger"/>
        <xs:element name="hire" type="xs:date"/>
        
        <xs:element name="telephone">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="cell" type="xs:string"/>
              <xs:element name="office" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        
        <xs:element name="facialPortrait" type="xs:hexBinary" />
        <xs:element name="subordinate" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema><taskdef 
  name="xjc"
  classname="com.sun.tools.xjc.XJCTask"
  classpath refid="class.path" />
</taskdef>
<target name="xsd2java">
  <xjc target="src" package="hoge.xsd">
    <schema dir="schema" includes="*.xsd"/>
    <produces dir="src/hoge/xsd" includes="**/*" />
  </xjc>
</target>Buildfile: C:\eclipse\workspace\JAXB\build.xml
xsd2java:
      [xjc] files are up to date
BUILD SUCCESSFUL
Total time: 13 seconds½êÄê¤Î¼ê³¤(1,2)¤òƧ¤à¤È¡¢¤½¤ì°Ê¹ß¤ÏÄ̾ï¤ÎBean¤ò°·¤¦¤Î¤ÈƱ¤¸¤Ç¤¹¡£
¥½¡¼¥¹¥³¡¼¥É:
public final class UnMarshalXML {
 public static void main(String[] args) throws JAXBException {
   // 1.XML¤ò²ò¼á¤¹¤ë¥¯¥é¥¹¤Î³ÊǼ¤µ¤ì¤Æ¤¤¤ë¥Ñ¥Ã¥±¡¼¥¸¤ò»ØÄꤷ¤ÆUnmarshaler¤òºî¤ë
   JAXBContext jc = JAXBContext.newInstance("hoge.xsd");
   Unmarshaller u = jc.createUnmarshaller();
   u.setValidating(false); // ¸·Ì©¤Ê¥Á¥§¥Ã¥¯¤ò¤·¤Ê¤¤(¼ê½ñ¤¤Î¤Ï¤Þ¤º°ú¤Ã¤«¤«¤ê¤Þ¤¹)
   // 2.Employee¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò»ý¤ÄBean¤ËXML¤ò¥Þ¥Ã¥Ô¥ó¥°
   Employee employee = (Employee) u.unmarshal(new File("Schema/Yamada.xml"));
   // 3.1.°Ê²¼getter/setter¤Î¤¢¤ëÉáÄ̤ÎBean¤È¤·¤ÆÃͤò¼è¤ê½Ð¤·¤Þ¤¹
   //     EmployeeType¤À¤±¸«¤ì¤Ð¤¤¤¤
   System.out.println("name\t:" + employee.getName());
   System.out.println("id\t:" + employee.getId());
   System.out.println("hired date\t:" + employee.getHire().getTime());
   // 3.2.Æþ¤ì»Ò¤ÏEmployeeType¤Î¥µ¥Ö¥¯¥é¥¹¤Ç¤¢¤ëTelephoneType¤Î·Á¤ÇÆþ¤Ã¤Æ¤¤¤ë
   TelephoneType tel = employee.getTelephone();
   System.out.println("tel.cell\t:" + tel.getCell());
   System.out.println("tel.office\t:" + tel.getOffice());
   // 3.3.byteÇÛÎó¤ò¼õ¤±¼è¤ê¤Þ¤¹
   byte[] pictureData = employee.getFacialPortrait();
   if (pictureData != null) {
     System.out.print("picture data\t:");
     for (int cnt = 0; cnt < pictureData.length; cnt++) {
       System.out.print(pictureData[cnt] + ",");
     }
     System.out.println();
   }
   // 3.4.minOccurs/maxOccurs¤ò»ØÄꤹ¤ë¤ÈList¤Ë¤Ê¤ë
   //     ¤É¤Î·¿¤ÇList¤Ë¤Ï¤¤¤Ã¤Æ¤¤¤ë¤«¤Ï¡¢hoge.xsd.EmployeeType¤ÎJavadoc¤ò»²¾È
   List subordinateList = employee.getSubordinate();
   if (subordinateList != null) {
     for (Iterator it = subordinateList.iterator(); it.hasNext();) {
       System.out.println("sub ordinate :" + (String) it.next());
     }
   }
 }
}
¼Â¹Ô·ë²Ì: name :YAMADA Taro id :1 hired date :Mon Jan 01 00:00:00 JST 2001 tel.cell :090-1234-5678 tel.office :03-9876-5432 picture data :1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, sub ordinate :SATO Hoshi sub ordinate :James Tiberius Kirk
¤Á¤Ê¤ß¤Ë
Marsha¡Ú̾¡Û¸µ¿ã¡¦»ÊÎá´± ¡Ú¼«Æ°¡Ûʤ֡¦À°Îó¤¹¤ë ¡Ú¾ư¡ÛÊ¤Ф»¤ë¡¦À°Î󤵤»¤ë¡¦Æ³¤¯
Unmarshaller¤È¤¤¤¦¤Î¤Ï¡¢½çÊÔÀ®¥Õ¥¡¥¤¥ë¤ò¥á¥â¥ê¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë¤è¤¦¤Ê¥¤¥á¡¼¥¸
¾å¤Ç¤â¾Ò²ð¤·¤¿¤è¤¦¤Ë¡¢¿·¤·¤¯¥ª¥Ö¥¸¥§¥¯¥È¤òºî¤ë¤È¤¤Ë¤Ïnew¤¹¤ë¤Î¤Ç¤Ï¤Ê¤¯¡¢
ObjectFactory?.create****()¤ò»È¤¦¡£
ºÇ¸å¤ËBean¤òXML²½¤¹¤ë¤È¤¤Ë¤ÏMarshaller¤ò»È¤¤¤Þ¤¹¡£º£²ó¤Ïɸ½à½ÐÎϤË
½ñ¤½Ð¤·¤Þ¤·¤¿¤¬¡¢FileOutputStream?¤Ë½ñ¤½Ð¤»¤Ð¥Õ¥¡¥¤¥ë¤Ë½ñ¤½Ð¤»¤Þ¤¹¡£
¥½¡¼¥¹¥³¡¼¥É:
public final class MarshalXML {
 public static void main(String[] args) throws JAXBException {
   // 1.¿·¤·¤¯JAXB¥ª¥Ö¥¸¥§¥¯¥È¤òºî¤ë¤È¤¤Ë¤Ï¡¢ObjectFactory¤ò»È¤¤¤Þ¤¹¡£
   //   ¥¯¥é¥¹Ì¾¤¬Æ±¤¸¤â¤Î¤¬¤¿¤¯¤µ¤ó¤¢¤ë¤Î¤Ç¡¢¥Õ¥ë¥Ñ¥¹¤Ç·¿Ì¾¤ò½ñ¤¤¤Æ¤ª¤¯¤Î¤¬µÈ
   hoge.xsd.ObjectFactory objFactory = new hoge.xsd.ObjectFactory();
   // 2.1.XMLʸ½ñ¤Ë¤Ê¤ëJAXB¥ª¥Ö¥¸¥§¥¯¥È¤Ï create***() ¤Çºî¤ê¤Þ¤¹
   Employee employee = objFactory.createEmployee();
   // 2.2.Æþ¤ì»Ò¤ÎJAXB¥ª¥Ö¥¸¥§¥¯¥È¤Ï create***Type() ¤Çºî¤ê¤Þ¤¹
   TelephoneType tel = objFactory.createEmployeeTypeTelephoneType();
   
   // 3.1.¸å¤Ïsetter¤ò»È¤Ã¤ÆÃͤòÆþ¤ì¤Æ¤¤¤¯¤À¤±
   employee.setName("ËÙ±ÒÌç");
   employee.setId(BigInteger.valueOf(2L));
   employee.setHire(Calendar.getInstance());
   employee.setFacialPortrait(new byte[] {(byte) 255, (byte) 254, (byte) 253});
   
   // 3.2.Æþ¤ì»Ò¤Î¾ì¹ç¤Ë¤âsetter¤ò»È¤¤¤Þ¤¹
   tel.setCell("080-1111-1111");
   tel.setOffice("03-9876-5432");
   employee.setTelephone(tel);
   
   // 3.3.List¤Î¾ì¹ç¤Ë¤Ï¡¢get*** ¤Ç¡¡List ¤ò¼èÆÀ¤·¤ÆÁàºî¤·¤Þ¤¹
   employee.getSubordinate().add("°ËÀª³¤Ï·Â¢");
   employee.getSubordinate().add("°ìÆó»°ÂÀÉ×");
   // 4.Ãͤνñ¤¹þ¤ß
   JAXBContext jc = JAXBContext.newInstance("hoge.xsd"); // ¥Ñ¥Ã¥±¡¼¥¸Ì¾
   Marshaller m = jc.createMarshaller();
   m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //²þ¹Ô¤òÆþ¤ì¤ë
   m.setProperty(Marshaller.JAXB_ENCODING, "Shift_JIS"); //ʸ»ú¥³¡¼¥É¤Î»ØÄê
   m.marshal(employee, System.out);
 }
}
¼Â¹Ô·ë²Ì:
<?xml version="1.0" encoding="Shift_JIS" standalone="yes"?>
<employee>
   <name>ËÙ±ÒÌç</name>
   <id>2</id>
   <hire>2005-01-20+09:00</hire>
   <telephone>
       <cell>080-1111-1111</cell>
       <office>03-9876-5432</office>
   </telephone>
   <facialPortrait>FFFEFD</facialPortrait>
   <subordinate>°ËÀª³¤Ï·Â¢</subordinate>
   <subordinate>°ìÆó»°ÂÀÉ×</subordinate>
</employee>
ÅöÁ³¡¢ÆÉ¤ß¹þ¤ó¤ÀXMLʸ½ñ¤ÎÆâÍÆ¤Î½ñ¤´¹¤¨¤â²Äǽ¤Ç¤¹¡£
°À¤â»ÒÍ×ÁǤâJAXB¤«¤é¤Î°·¤¤¤ÏƱ¤¸¡£
<length unit="m">1.70</length>
            ¢
public class LengthType{
  String getUnit();
  void setUnit(String unit);
  double getValue();
  void setValue(double value);
}
 JAXB3.png 3485·ï
[¾ÜºÙ]
JAXB3.png 3485·ï
[¾ÜºÙ]
  JAXB2.png 3348·ï
[¾ÜºÙ]
JAXB2.png 3348·ï
[¾ÜºÙ]
  JAXB_Classes.png 3323·ï
[¾ÜºÙ]
JAXB_Classes.png 3323·ï
[¾ÜºÙ]
  JAXB1.png 3312·ï
[¾ÜºÙ]
JAXB1.png 3312·ï
[¾ÜºÙ]