java學習

          java學習

           

          使用jaxb完成對象和xml的轉換

          學生類

          import javax.xml.bind.annotation.XmlRootElement;

          @XmlRootElement
          public class Student {
           private int id;
           private String name;
           private int age;
           private Classroom classroom;
           public int getId() {
            return id;
           }
           public void setId(int id) {
            this.id = id;
           }
           public String getName() {
            return name;
           }
           public void setName(String name) {
            this.name = name;
           }
           public int getAge() {
            return age;
           }
           public void setAge(int age) {
            this.age = age;
           }
           public Classroom getClassroom() {
            return classroom;
           }
           public void setClassroom(Classroom classroom) {
            this.classroom = classroom;
           }
           public Student(int id, String name, int age, Classroom classroom) {
            super();
            this.id = id;
            this.name = name;
            this.age = age;
            this.classroom = classroom;
           }
           public Student() {
            super();
           }
           
           
          }

          教室類
          public class Classroom {
           private int id;
           private String name;
           private int grade;
           public int getId() {
            return id;
           }
           public void setId(int id) {
            this.id = id;
           }
           public String getName() {
            return name;
           }
           public void setName(String name) {
            this.name = name;
           }
           public int getGrade() {
            return grade;
           }
           public void setGrade(int grade) {
            this.grade = grade;
           }
           public Classroom(int id, String name, int grade) {
            super();
            this.id = id;
            this.name = name;
            this.grade = grade;
           }
           public Classroom() {
            super();
            // TODO Auto-generated constructor stub
           }
           
           
          }

          測試類:

          public class TestJaxb {

              public static void main(String[] args){
                  TestJaxb.test02();
              }
           @Test
           public static void test01() {//測試對象轉換xml字符串
            try {
             JAXBContext ctx = JAXBContext.newInstance(Student.class);
             Marshaller marshaller = ctx.createMarshaller();
             Student stu = new Student(1,"張三",21,new Classroom(1,"10計算機應用技術",2010));
             marshaller.marshal(stu, System.out);
            } catch (JAXBException e) {
             e.printStackTrace();
            }
           }
           
           @Test
           public static void test02() {//測試xml字符串轉換對象
            try {
             String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><student><age>21</age><classroom><grade>2010</grade><id>1</id><name>10計算機應用技術</name></classroom><id>1</id><name>張三</name></student>";
             JAXBContext ctx = JAXBContext.newInstance(Student.class);
             Unmarshaller um = ctx.createUnmarshaller();
             Student stu = (Student)um.unmarshal(new StringReader(xml));
             System.out.println(stu.getName()+","+stu.getClassroom().getName());
            } catch (JAXBException e) {
             e.printStackTrace();
            }
            
           }
          }


          posted on 2013-08-07 11:02 楊軍威 閱讀(860) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 伊川县| 乳山市| 娱乐| 紫阳县| 诏安县| 连城县| 灵武市| 博爱县| 兴义市| 双江| 东乡县| 象山县| 绩溪县| 虞城县| 宁南县| 永昌县| 壶关县| 时尚| 彭阳县| 镇坪县| 大理市| 龙州县| 湘乡市| 莱州市| 清河县| 阿坝县| 萝北县| 中西区| 汤阴县| 策勒县| 柘城县| 凤庆县| 凤城市| 郁南县| 丰县| 高唐县| 儋州市| 巴南区| 福泉市| 东宁县| 法库县|