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)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 尤溪县| 姚安县| 乡城县| 卓尼县| 天水市| 合江县| 遵义市| 二连浩特市| 大姚县| 天门市| 类乌齐县| 修水县| 奉新县| 黄冈市| 安仁县| 威远县| 龙门县| 临高县| 开封县| 泰和县| 江华| 喀什市| 绥阳县| 宿州市| 武安市| 富宁县| 瑞安市| 巴马| 抚松县| 乌恰县| 兖州市| 甘德县| 长寿区| 营口市| 新田县| 陆河县| 陆丰市| 永兴县| 古丈县| 高安市| 敦化市|