JAXB实现java转xml的demo【转】

demo1:

转自:http://hbiao68.iteye.com/blog/1958413

下面使用的是JDK自带的类,没有引用任何第三方jar包。

 

Unmarshaller 类使客户端应用程序能够将 XML 数据转换为 Java 内容对象树。

备注:marshal(序列化、排列、整理)

Marshaller 类使客户端应用程序能够将 Java 内容树转换回 XML 数据。

 

Java代码  收藏代码
  1. package hb.jaxb;  
  2.   
  3. public class Classroom {  
  4.     private int id;  
  5.     private String name;  
  6.     private int grade;  
  7.   
  8.     public int getId() {  
  9.         return id;  
  10.     }  
  11.   
  12.     public void setId(int id) {  
  13.         this.id = id;  
  14.     }  
  15.   
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.   
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.   
  24.     public int getGrade() {  
  25.         return grade;  
  26.     }  
  27.   
  28.     public void setGrade(int grade) {  
  29.         this.grade = grade;  
  30.     }  
  31.   
  32.     public Classroom(int id, String name, int grade) {  
  33.         super();  
  34.         this.id = id;  
  35.         this.name = name;  
  36.         this.grade = grade;  
  37.     }  
  38.   
  39.     public Classroom() {  
  40.         super();  
  41.     }  
  42.   
  43. }  

 

Java代码  收藏代码
  1. package hb.jaxb;  
  2.   
  3. import javax.xml.bind.annotation.XmlRootElement;  
  4.   
  5. @XmlRootElement  
  6. public class Student {  
  7.     private int id;  
  8.     private String name;  
  9.     private int age;  
  10.     private Classroom classroom;  
  11.   
  12.     public int getId() {  
  13.         return id;  
  14.     }  
  15.   
  16.     public void setId(int id) {  
  17.         this.id = id;  
  18.     }  
  19.   
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.   
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.   
  28.     public int getAge() {  
  29.         return age;  
  30.     }  
  31.   
  32.     public void setAge(int age) {  
  33.         this.age = age;  
  34.     }  
  35.   
  36.     public Classroom getClassroom() {  
  37.         return classroom;  
  38.     }  
  39.   
  40.     public void setClassroom(Classroom classroom) {  
  41.         this.classroom = classroom;  
  42.     }  
  43.   
  44.     public Student(int id, String name, int age, Classroom classroom) {  
  45.         super();  
  46.         this.id = id;  
  47.         this.name = name;  
  48.         this.age = age;  
  49.         this.classroom = classroom;  
  50.     }  
  51.   
  52.     //无参够着函数一定需要,否则JXBContext无法正常解析。  
  53.     public Student() {  
  54.         super();  
  55.     }  
  56. }  

 

注意:

1、需要转换的model对象一定要添加@XmlRootElement注解,其里面的其他对象则不需要

2、需要转换的model对象一定要有不带参数的构造方法,包括该对象里面引用的对象。

 

Java代码  收藏代码
  1. package hb.jaxb;  
  2.   
  3. import java.io.StringReader;  
  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 org.junit.Test;  
  10.   
  11. public class TestJaxb {  
  12.   
  13.     @Test  
  14.     public void beanToXML() {  
  15.         Classroom classroom = new Classroom(1, "软件工程", 4);  
  16.         Student student = new Student(101, "张三", 22, classroom);  
  17.   
  18.         try {  
  19.             JAXBContext context = JAXBContext.newInstance(Student.class);  
  20.             Marshaller marshaller = context.createMarshaller();  
  21.             marshaller.marshal(student, System.out);  
  22.         } catch (JAXBException e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.   
  26.     }  
  27.       
  28.     @Test  
  29.     public void XMLStringToBean(){  
  30.         String xmlStr = "<?xml version=\"1.0 \" encoding=\"UTF-8\" standalone=\"yes\"?><student><age> 22</age><classroom><grade>4< /grade><id>1</id><name>软件工程</name>< /classroom><id>101</id><name>张三</name>< /student>";  
  31.         try {  
  32.             JAXBContext context = JAXBContext.newInstance(Student.class);  
  33.             Unmarshaller unmarshaller = context.createUnmarshaller();  
  34.             Student student = (Student)unmarshaller.unmarshal(new StringReader(xmlStr));  
  35.             System.out.println(student.getAge());  
  36.             System.out.println(student.getClassroom().getName());  
  37.         } catch (JAXBException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.           
  41.     }  
  42. }  

 

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到 XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。

 

    JAXBContext 类提供到 JAXB API 的客户端入口点。它提供了管理实现 JAXB 绑定框架操作所需的 XML/Java 绑定信息的抽象,这些操作包括:解组、编组和验证。

 

 

 

 


 

demo2:

转自:http://www.blogjava.net/kangdy/archive/2011/11/23/364635.html

code1: colors类  根节点

code1
package com.kangdy.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Colors")
@XmlAccessorType(XmlAccessType.FIELD)
public class Colors {
    
    @XmlElement(name = "red",nillable=true)
    private Red red;
    
    @XmlElement(name = "blue",nillable=true)
    private Blue blue;

    public Red getRed() {
        return red;
    }

    public Blue getBlue() {
        return blue;
    }

    public void setRed(Red red) {
        this.red = red;
    }

    public void setBlue(Blue blue) {
        this.blue = blue;
    }
}


code2:  Red类  子节点

code2package com.kangdy.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "red")
@XmlAccessorType(XmlAccessType.FIELD)
public class Red {
    
    private String value;
    
    @XmlAttribute(name = "att1")
    private String att;
    
    public String getValue() {
        return value;
    }
    
    public void setValue(String value) {
        this.value = value;
    }

    public String getAtt() {
        return att;
    }

    public void setAtt(String att) {
        this.att = att;
    }
    
}


code3:  类 Blue 子节点

code3
package com.kangdy.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "blue")
@XmlAccessorType(XmlAccessType.FIELD)
public class Blue {
    private String value;
    
    @XmlAttribute(name = "att2")
    private String att2;
    
    @XmlAttribute(name = "att1")
    private String att;
    
    public String getAtt() {
        return att;
    }

    public void setAtt(String att) {
        this.att = att;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getAtt2() {
        return att2;
    }

    public void setAtt2(String att2) {
        this.att2 = att2;
    }
}


code4: main类

code4
package com.kangdy.test;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Jaxbtest {
    public static void main(String[] args) throws Exception {

        StringWriter writer = new StringWriter();
        JAXBContext jc = JAXBContext.newInstance(Colors.class);
        Marshaller ma = jc.createMarshaller();
        ma.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        
        Colors colors = new Colors();
        Red red = new Red();
        red.setAtt("att-red");
        red.setValue("red");
        Blue blue = new Blue();
        blue.setValue("blue");
        blue.setAtt("att-blue");
        blue.setAtt2("blue-att2");
        colors.setRed(red);
        colors.setBlue(blue);
        
        ma.marshal(colors, writer);
        System.out.println(writer.toString());

    }
}


运行结果:

结果
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Colors>
    <red att1="att-red">
        <value>red</value>
    </red>
    <blue att1="att-blue" att2="blue-att2">
        <value>blue</value>
    </blue>
</Colors>
 
 
posted @ 2015-08-06 17:55  java后端领域  阅读(242)  评论(0)    收藏  举报