夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          package test;

          import java.io.File;
          import java.io.FileOutputStream;
          import java.io.StringReader;
          import java.io.StringWriter;

          import javax.xml.bind.JAXBContext;
          import javax.xml.bind.Marshaller;
          import javax.xml.bind.Unmarshaller;
          import javax.xml.bind.annotation.XmlRootElement;

          @XmlRootElement
          public class HelloWorld {

            
          private String hello;
            
          private Integer world;

            
          public String getHello() {
              
          return hello;
            }


            
          public void setHello(String hello) {
              
          this.hello = hello;
            }


            
          public Integer getWorld() {
              
          return world;
            }


            
          public void setWorld(Integer world) {
              
          this.world = world;
            }


            
          public String toString() {
              
          return hello + "-" + world;
            }


            
          public static void main(String[] args) throws Exception {

              
          // Set up file and JAXB context
              final File file = new File("hello.xml");
              
              JAXBContext context 
          = JAXBContext.newInstance(HelloWorld.class);

              
          // Creates a HelloWorld object
              HelloWorld hw = new HelloWorld();
              hw.setHello(
          "Hello !!!");
              hw.setWorld(
          1234);

              
          // From a HelloWorld object creates a hello.xml file
              Marshaller m = context.createMarshaller();
              StringWriter sw 
          = new StringWriter();
              m.marshal(hw, sw);
              System.out.println(sw.toString());
              m.marshal(hw, 
          new FileOutputStream(file));

              
          // From the hello.xml file creates a HelloWorld object
              Unmarshaller um = context.createUnmarshaller();
              StringReader sr 
          = new StringReader(sw.toString());
              HelloWorld hw2 
          = (HelloWorld) um.unmarshal(sr);
              System.out.println(hw2);

            }

          }


          posted on 2010-09-25 18:25 HUIKK 閱讀(203) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 阿克苏市| 南岸区| 海淀区| 灵宝市| 文登市| 芮城县| 同仁县| 定陶县| 当涂县| 太保市| 普安县| 易门县| 延安市| 房产| 社旗县| 巴马| 嘉黎县| 垫江县| 宁南县| 石棉县| 赤壁市| 开原市| 上栗县| 南投县| 陵川县| 苍梧县| 永安市| 宣武区| 太康县| 灵丘县| 邹平县| 疏勒县| 云安县| 无为县| 兴宁市| 平泉县| 苍山县| 万载县| 九龙城区| 伊川县| 内乡县|