码迷,mamicode.com
首页 > 编程语言 > 详细

Java: xml转换

时间:2016-06-23 12:25:43      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

java对于xml的转换有很多种,比较有名的有:DOM, DOM4J, JDOM, SAX。这里要介绍的是javax.xml包的对xml文件的转换。相比于前面几种是最简单的。

直接上代码:

Student.java:

技术分享
 1 @XmlRootElement
 2 public class Student {
 3     private String name;
 4     private String width;
 5     private String height;
 6     private int age;
 7     public Student() {
 8         super();
 9     }
10     public Student(String name, String width, String height, int age) {
11         super();
12         this.name = name;
13         this.width = width;
14         this.height = height;
15         this.age = age;
16     }
17     public String getName() {
18         return name;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     public String getWidth() {
24         return width;
25     }
26     public void setWidth(String width) {
27         this.width = width;
28     }
29     public String getHeight() {
30         return height;
31     }
32     public void setHeight(String height) {
33         this.height = height;
34     }
35     public int getAge() {
36         return age;
37     }
38     public void setAge(int age) {
39         this.age = age;
40     }
41     @Override
42     public String toString() {
43         return "Student [name=" + name + ", width=" + width + ", height="
44                 + height + ", age=" + age + "]";
45     }
46     
47 }
View Code

BaseTest.java:

 1 public class BaseTest {
 2     @Test
 3     public void test01() throws JAXBException {
 4         JAXBContext jc = JAXBContext.newInstance(Student.class);
 5         Marshaller ms = jc.createMarshaller();
 6         ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 7         ms.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
 8         Student st = new Student("zhang", "w", "h", 11);
 9         ms.marshal(st, new File("student.xml"));
10         
11         StringWriter writer = new StringWriter();
12         ms.marshal(st, writer);
13         System.out.println(writer.toString());
14     }
15     @Test
16     public void test02() throws JAXBException {
17         String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
18                 + "<student><age>11</age><height>h</height><name>zhang</name><width>w</width></student>";  
19         JAXBContext jc = JAXBContext.newInstance(Student.class);
20         Unmarshaller unmar = jc.createUnmarshaller();
21         File f = new File("student.xml");
22         Student st = (Student)unmar.unmarshal(f);
23         System.out.println(st);
24         Student st1 = (Student)unmar.unmarshal(new StringReader(xml));
25         System.out.println(st1);
26     }
27 }

@XmlRootElement即是对该类进行反射映射为xml。

如果没有@XmlElement,默认对Property进行反射,xml node的名字与该属性名相同,如果不同,需要加上@XmlElement

如果有@XmlElement,注意该注解必须放与get方法之前。

RootClass:

技术分享
 1 @XmlRootElement(name="rootclass")
 2 public class RootClass {
 3     
 4     private EleClassA a;
 5     
 6     private EleClassB b;
 7     
 8     private String root;
 9     
10     private String rootA;
11     private String transientField;
12     
13     public RootClass() {
14         super();
15     }
16     public RootClass(String transientField) {
17         //super();
18         this.transientField = transientField;
19     }
20     private List<String> items;
21     
22     @XmlElement(name="eleClassA")
23     public EleClassA getA() {
24         return a;
25     }
26     public void setA(EleClassA a) {
27         this.a = a;
28     }
29     @XmlElement(name="EleClassA")
30     public EleClassB getB() {
31         return b;
32     }
33     public void setB(EleClassB b) {
34         this.b = b;
35     }
36     public String getRoot() {
37         return root;
38     }
39     public void setRoot(String root) {
40         this.root = root;
41     }
42     @XmlTransient
43     public String getRootA() {
44         return rootA;
45     }
46     public void setRootA(String rootA) {
47         this.rootA = rootA;
48     }
49     @XmlElementWrapper(name="items")
50     @XmlElement(name="item")
51     public List<String> getItems() {
52         return items;
53     }
54     public void setItems(List<String> items) {
55         this.items = items;
56     }
57 }
View Code

EleClassA:

技术分享
 1 public class EleClassA {
 2     private String eleA;
 3     private String eleB;
 4     private String attrC;
 5     @XmlElement
 6     public String getEleA() {
 7         return eleA;
 8     }
 9     public void setEleA(String eleA) {
10         this.eleA = eleA;
11     }
12     @XmlElement(name="elebnewname")
13     public String getEleB() {
14         return eleB;
15     }
16     public void setEleB(String eleB) {
17         this.eleB = eleB;
18     }
19     @XmlAttribute
20     public String getAttrC() {
21         return attrC;
22     }
23     public void setAttrC(String attrC) {
24         this.attrC = attrC;
25     }
26     
27 }
View Code

EleClassB:

技术分享
 1 public class EleClassB {
 2     private String attrUserName;
 3     private String attrPassword;
 4     private String eleCode;
 5     @XmlAttribute
 6     public String getAttrUserName() {
 7         return attrUserName;
 8     }
 9     public void setAttrUserName(String attrUserName) {
10         this.attrUserName = attrUserName;
11     }
12     @XmlAttribute(name="password")
13     public String getAttrPassword() {
14         return attrPassword;
15     }
16     public void setAttrPassword(String attrPassword) {
17         this.attrPassword = attrPassword;
18     }
19     @XmlElement
20     public String getEleCode() {
21         return eleCode;
22     }
23     public void setEleCode(String eleCode) {
24         this.eleCode = eleCode;
25     }
26 }
View Code

AttributeTest:

 1 public class AttributeTest {
 2 
 3     @Test
 4     public void test01() throws JAXBException {
 5         RootClass rc = new RootClass("transient");
 6         //RootClass rc = new RootClass();
 7         EleClassA a = new EleClassA();
 8         EleClassB b = new EleClassB();
 9         
10         a.setAttrC("attrC");
11         a.setEleA("eleA");
12         a.setEleB("eleB");
13         
14         b.setAttrPassword("attrPassword");
15         b.setAttrUserName("attrUsrName");
16         b.setEleCode("eleCode");
17         
18         List<String> l = new ArrayList<String>();
19         l.add("yingzhongwen");
20         l.add("hetao");
21         l.add("xinwang");
22         
23         rc.setA(a);
24         rc.setB(b);
25         rc.setRoot("root");
26         rc.setRootA("rootA");
27         rc.setItems(l);
28         
29         JAXBContext context = JAXBContext.newInstance(RootClass.class);
30         Marshaller mar = context.createMarshaller();
31         mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
32         mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
33         
34         StringWriter writer = new StringWriter();
35         mar.marshal(rc, writer);
36         System.out.println(writer.toString());
37     }
38 }

 

Java: xml转换

标签:

原文地址:http://www.cnblogs.com/yingzhongwen/p/5610096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!