夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            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
          主站蜘蛛池模板: 永吉县| 临安市| 洛隆县| 娄底市| 博爱县| 新乡市| 江北区| 自治县| 周至县| 棋牌| 卢湾区| 京山县| 墨竹工卡县| 图木舒克市| 蒙城县| 寿光市| 鱼台县| 安多县| 安岳县| 宜都市| 济南市| 宝清县| 河南省| 公主岭市| 涞水县| 布拖县| 阳新县| 太湖县| 黄大仙区| 沙洋县| 疏勒县| 南昌县| 香格里拉县| 广汉市| 正定县| 修水县| 东兰县| 合阳县| 乌兰县| 高尔夫| 延吉市|