¢¨ Java 5 °Ê¹ß¤Ê¤é¤Ð JAXB2 ¤ÎÊý¤¬¤ª¤¹¤¹¤á


¥¤¥ó¥¹¥È¡¼¥ë

  1. Java Web Services Developer Pack¤Î¼èÆÀ
    http://java.sun.com/xml/download.html
    ¤«¤é¡¢jwsdp-1_5-windows-i586.exe ¤ò¥À¥¦¥ó¥í¡¼¥É¤·¤Æ¤­¤Æ¥¤¥ó¥¹¥È¡¼¥ë¤·¤Þ¤¹¡£ JAXB°Ê³°¤âXML´Ø·¸¤Î¥é¥¤¥Ö¥é¥ê¤¬¥¤¥ó¥¹¥È¡¼¥ë¤µ¤ì¤Þ¤¹¡£ ¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï¡¢C:\Sun\jwsdp-1.5 ¤Ë¥¤¥ó¥¹¥È¡¼¥ë¤µ¤ì¤ë¤Ï¤º¤Ç¤¹¡£
  2. Eclipse¤«¤é¤Î¥¯¥é¥¹¥Ñ¥¹¤ÎÀßÄê
    ¥µ¥ó¥×¥ë¤ò¸«¤ë¤È¡¢
    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¤È¤·¤Æ¥×¥í¥¸¥§¥¯¥È¤Ë¥¤¥ó¥Ý¡¼¥È¤·¤Þ¤¹
    • WEB¥¢¥×¥ê¤äGUI¥¢¥×¥ê¤òJAR¤Ç¸Ç¤á¤ÆÇÛÉÛ¤¹¤ë¤Ê¤É¤Î¾ì¹ç¤Ï¡¢/WEB-INF/lib ¤Ë¥³¥Ô¡¼¤·¤¿Êý¤¬¥¹¥Þ¡¼¥È¤«¤â

²òÀÏÂоݤÎXML/XMLSchema

ËÜ¥µ¥¤¥È¤ÎXMLSchema¤Ë´Ø¤¹¤ë¥á¥â?¤Çºî¤Ã¤¿XMLSchema¤ò¿¾¯Ê£»¨¤Ë¤·¤ÆÁàºî¤·¤Þ¤¹¡£

  1. id¤¬ positiveInteger·¿ ¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹
  2. hire¤¬ date·¿ ¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹
  3. telephone¤Ï Æþ¤ì»Ò ¤ÎÍ×ÁǤǤ¹
  4. facialPortrait¤¬ hexBinary·¿ ¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹(´é¼Ì¿¿(JPEG))
  5. subordinate¤¬ ·«¤êÊÖ¤·¹àÌÜ ¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹
    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>

XML-Java¥Ð¥¤¥ó¥Ç¥£¥ó¥°¥×¥í¥°¥é¥à¤ÎÀ¸À®

  1. ant¤ÎÀßÄê
    ¥µ¥ó¥×¥ë(C:\Sun\jwsdp-1.5\jaxb\sample)¤È¥É¥­¥å¥á¥ó¥È(C:\Sun\jwsdp-1.5\jaxb\docs)¤ò»²¹Í¤Ë¤·¤Æºî¤ê¤Þ¤¹¡£
    ¾ÜºÙ¤ÏAnt ¥¿¥¹¥¯¿÷·¿½¸#xsd2java¤ò»²¾È
    <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>
  2. ant¥¿¥¹¥¯¤Î¼Â¹Ô
    ¾åµ­¤Î¥¿¥¹¥¯¤ò¼Â¹Ô¤¹¤ë¤È¡¢schema¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤¢¤ë example.xsd ¤«¤é employee·¿ ¤ò²òÀϤ¹¤ë¥×¥í¥°¥é¥à¤¬À¸À®¤µ¤ì¤Þ¤¹¡£
    Buildfile: C:\eclipse\workspace\JAXB\build.xml
    xsd2java:
          [xjc] files are up to date
    BUILD SUCCESSFUL
    Total time: 13 seconds
  3. À¸À®·ë²Ì
    1. ­¡¤ò¸«¤ë¤È hoge.xsd °Ê²¼¤Ë¤¿¤¯¤µ¤óJava¥½¡¼¥¹¤¬À¸À®¤µ¤ì¤Æ¤¤¤Þ¤¹
    2. ÍøÍѤ¹¤ë¤¦¤¨¤Ç´Ø·¸¤¬¤¢¤ë¤Î¤Ï­¢EmployeeType?¤È­£ObjectFactory?¤À¤±¤Ç¤¹
      1. ­¢EmployeeType?¤Ë¤Ïgetter¤Èsetter¤¬¤¢¤ê¡¢¥¯¥é¥¤¥¢¥ó¥È¥×¥í¥°¥é¥à¤Ï ¤Á¤ç¤Ã¤È¤·¤¿¼ê½ç¤òƧ¤ó¤À¸å¤ËXML¤òÄ̾ï¤ÎJavaBean?¤Î¤è¤¦¤Ë°·¤¨¤Þ¤¹¡£
      2. ­£¤½¤Î¤Á¤ç¤Ã¤È¤·¤¿¼ê½ç¤Ï¡¢ObjectFactory?Åù¤Ë±£Ê䵤ì¤Þ¤¹¡£
        ¥¯¥é¥¤¥¢¥ó¥È¥×¥í¥°¥é¥à¤«¤é¸«¤ë¤È¡¢new ¤¹¤ëÂå¤ï¤ê¤Ë¡¢ ObjectFacotory?.createXXXX()¤ò¼Â¹Ô¤¹¤ë¡£
         
        ­¡­¢­£
        JAXB1.pngJAXB2.pngJAXB3.png
        ­¤
        JAXB_Classes.png
         
    3. ­¤¤Ï¥¯¥é¥¹Á´ÂοޤǤ¹
      1. XML¤ò¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤Ï new ¤ÎÂå¤ï¤ê¤Ë ObjectFactory?.create****() ¤ò»È¤Ã¤Æ¥¤¥ó¥¹¥¿¥ó¥¹²½¤·¤Þ¤¹¡£
      2. ¥¤¥ó¥¹¥¿¥ó¥¹²½»þ¤Ë¤Ï¡¢EmployeeImpl?¤¬ºî¤é¤ì¡¢¤¤¤í¤¤¤í¤È½é´ü²½½èÍý¤¬¤µ¤ì¤ë¤è¤¦¤Ç¤¹¡£
      3. ¥æ¡¼¥¶¥×¥í¥°¥é¥à¤ËObjectFactory?.create****()¤«¤éÊÖ¤µ¤ì¤ë¤Î¤Ï¡¢Employee¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Ç¤¹¡£
      4. ¥æ¡¼¥¶¥×¥í¥°¥é¥à¤«¤é¤ÏEmployee¥¤¥ó¥¿¥Õ¥§¡¼¥¹¡Ê¤Îsetter/getter)¤ò»È¤Ã¤Æ¡¢XMLʸ½ñ¤òÁàºî¤·¤Þ¤¹¡£

XMLÆɤ߹þ¤ß¥¯¥é¥¹¤Î¼ÂÁõ

½êÄê¤Î¼ê³¤­(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¤È¤¤¤¦¤Î¤Ï¡¢½çÊÔÀ®¥Õ¥¡¥¤¥ë¤ò¥á¥â¥ê¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë¤è¤¦¤Ê¥¤¥á¡¼¥¸

XML½ñ¤­¹þ¤ß¥¯¥é¥¹¤Î¼ÂÁõ

¾å¤Ç¤â¾Ò²ð¤·¤¿¤è¤¦¤Ë¡¢¿·¤·¤¯¥ª¥Ö¥¸¥§¥¯¥È¤òºî¤ë¤È¤­¤Ë¤Ï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ʸ½ñ¤ÎÆâÍƤνñ¤­´¹¤¨¤â²Äǽ¤Ç¤¹¡£

  1. XMLʸ½ñ¤ò Unmarshaller ¤Ç Bean¤ËÆɤ߹þ¤ó¤Ç
  2. Bean ¤ÎÆâÍƤò setter ¤ò»È¤Ã¤Æ½ñ¤­´¹¤¨¤Æ¡¢
  3. Marshaller ¤Ç Bean ¤ÎÆâÍƤò¥Õ¥¡¥¤¥ë¤Ë½ñ¤­½Ð¤¹

°À­¤Î°·¤¤

°À­¤â»ÒÍ×ÁǤâJAXB¤«¤é¤Î°·¤¤¤ÏƱ¤¸¡£

<length unit="m">1.70</length>
            ¢­
public class LengthType{
  String getUnit();
  void setUnit(String unit);
  double getValue();
  void setValue(double value);
}

»²¹Íʸ¸¥

  1. Sun, 2004, The Java Web Service Tutorial, http://java.sun.com/xml/download.html , 25-86 pp.
  2. ¸¶ÅÄÍλÒ, @IT, 2003, JAXB¤Ç¥Ç¡¼¥¿¥Ð¥¤¥ó¥Ç¥£¥ó¥°¤ËÄ©À路¤è¤¦, http://www.atmarkit.co.jp/fxml/rensai2/xmltool02/01.html
  3. muimi, 2003, JAXB¥á¥â, http://muimi.com/j/jaxb/
  4. ËÜ¥µ¥¤¥È¤ÎXML XMLSchema

Java


źÉÕ¥Õ¥¡¥¤¥ë: fileJAXB3.png 3229·ï [¾ÜºÙ] fileJAXB2.png 3091·ï [¾ÜºÙ] fileJAXB_Classes.png 3079·ï [¾ÜºÙ] fileJAXB1.png 3053·ï [¾ÜºÙ]

¥È¥Ã¥×   ÊÔ½¸ Åà·ë º¹Ê¬ ¥Ð¥Ã¥¯¥¢¥Ã¥× źÉÕ Ê£À½ ̾Á°Êѹ¹ ¥ê¥í¡¼¥É   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS   sitemap
Last-modified: 2006-02-18 (ÅÚ) 13:37:11 (6864d)
Short-URL:
ISBN10
ISBN13
9784061426061