【WebService】使用jaxb完成对象和xml的转换

 1 package com.slp.jxmarshaller;
 2 
 3 /**
 4  * Created by sanglp on 2017/2/26.
 5  */
 6 public class ClassName {
 7     private int id;
 8     private String name;
 9     private int grade;
10 
11     public ClassName() {
12     }
13 
14     public ClassName(int id, String name, int grade) {
15         this.id = id;
16         this.name = name;
17         this.grade = grade;
18     }
19 
20     public int getId() {
21         return id;
22     }
23 
24     public void setId(int id) {
25         this.id = id;
26     }
27 
28     public String getName() {
29         return name;
30     }
31 
32     public void setName(String name) {
33         this.name = name;
34     }
35 
36     public int getGrade() {
37         return grade;
38     }
39 
40     public void setGrade(int grade) {
41         this.grade = grade;
42     }
43 }
package com.slp.jxmarshaller;

import javax.xml.bind.annotation.XmlRootElement;

/**
 * Created by sanglp on 2017/2/26.
 * [com.sun.istack.internal.SAXException2: 由于类型 "com.slp.jxmarshaller.Student" 缺少 @XmlRootElement 注释, 无法将该类型编集为元素]
 */
@XmlRootElement
public class Student {
    private int id;
    private String name;
    private int age;
    private ClassName className;

    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 ClassName getClassName() {
        return className;
    }

    public void setClassName(ClassName className) {
        this.className = className;
    }

    public Student(int id, String name, int age, ClassName className) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.className = className;
    }

    public Student() {
    }
}
 1 package com.slp.jxmarshaller;
 2 
 3 import org.junit.Test;
 4 
 5 import javax.xml.bind.JAXBContext;
 6 import javax.xml.bind.JAXBException;
 7 import javax.xml.bind.Marshaller;
 8 import javax.xml.bind.Unmarshaller;
 9 import java.io.StringReader;
10 
11 /**
12  * Created by sanglp on 2017/2/26.
13  */
14 public class TestJaxb {
15 
16     /**
17      * 输出结果
18      * <?xml version="1.0" encoding="UTF-8" standalone="yes"?><student><age>21</age><className><grade>2010</grade><id>1</id><name>10数学</name></className><id>1</id><name>张三</name></student>
19      */
20     @Test
21     public void test01(){
22         try {
23             JAXBContext ctx = JAXBContext.newInstance(Student.class);
24             Marshaller marshaller = ctx.createMarshaller();
25             Student student = new Student(1,"张三",21,new ClassName(1,"10数学",2010));
26             marshaller.marshal(student,System.out);
27         } catch (JAXBException e) {
28             e.printStackTrace();
29         }
30     }
31 
32 
33     /**
34      * 张三 10数学
35      */
36     @Test
37     public void test02(){
38         String str="<?xml version="1.0" encoding="UTF-8" standalone="yes"?><student><age>21</age><className><grade>2010</grade><id>1</id><name>10数学</name></className><id>1</id><name>张三</name></student>
";
39         try {
40             JAXBContext ctx = JAXBContext.newInstance(Student.class);
41             Unmarshaller unmarshaller = ctx.createUnmarshaller();
42             Student stu = (Student) unmarshaller.unmarshal(new StringReader(str));
43             System.out.println(stu.getName()+" "+stu.getClassName().getName());
44         } catch (JAXBException e) {
45             e.printStackTrace();
46         }
47 
48     }
49 }
原文地址:https://www.cnblogs.com/dream-to-pku/p/6445282.html