1. 程式人生 > >實體轉xml(史上最全面)

實體轉xml(史上最全面)

記得之前遇到過需要將實體轉成xml,然後呼叫webservice介面的情景。比較了幾種方式,發現使用jdk本身自帶的JAXBContext轉換比較方便,直接通過註解來標識各個欄位在xml中的屬性及節點。下面直接上乾貨。

import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;


/**
 * 2016-05-12
 * @author yibeiqingfeng
 * Javabean 轉 xml
 */
public class XmlUtil {
	
	public static String beanToXml(Object obj, java.lang.Class<?> load)
			throws JAXBException {
		JAXBContext context = JAXBContext.newInstance(load);
		Marshaller marshaller = context.createMarshaller();
		//格式化輸出xml
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		//設定輸出xml編碼格式
		marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
		StringWriter writer = new StringWriter();
		marshaller.marshal(obj, writer);
		//去掉standalone屬性
		return writer.toString().replace("standalone=\"yes\"", "");
	}
	
}
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;




@XmlRootElement(name="student")
@XmlType(propOrder={"name","age","_class","specialitys"})
public class Student {
	private String name;
	
	private String age;
	
	private Class _class;
	
	private List<String> specialitys;
	
	@XmlAttribute(name="name")
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}
	
	@XmlAttribute(name="age")
	public String getAge() {
		return age;
	}


	public void setAge(String age) {
		this.age = age;
	}
	
	@XmlElement(name="class")
	public Class get_class() {
		return _class;
	}


	public void set_class(Class _class) {
		this._class = _class;
	}
	
	@XmlElementWrapper(name = "specialitys")
	@XmlElement(name = "speciality")
	public List<String> getSpecialitys() {
		return specialitys;
	}


	public void setSpecialitys(List<String> specialitys) {
		this.specialitys = specialitys;
	}
}

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;


public class Class {
	
	private String grade;
	
	private String value;
	
	@XmlAttribute(name="grade")
	public String getGrade() {
		return grade;
	}


	public void setGrade(String grade) {
		this.grade = grade;
	}
	
	@XmlValue
	public String getValue() {
		return value;
	}


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


   測試程式:
import java.util.ArrayList;
import java.util.List;


import javax.xml.bind.JAXBException;


/**
 * 2018-01-06
 * @author yibenqingfeng
 * test
 */
public class TestRun {
	
	public static void main(String[] args) {
		Student student = new Student();
		com.student.Class _class = new Class();
		student.setAge("13");
		student.setName("yibeiqingfeng");
		List<String> specialitys = new ArrayList<String>();
		specialitys.add("足球");
		specialitys.add("籃球");
		student.setSpecialitys(specialitys);
		_class.setGrade("primary");
		_class.setValue("2");
		student.set_class(_class);
		
		try {
			System.out.println(XmlUtil.beanToXml(student,Student.class));
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		
	}


}


  這個例子基本包含了xml的所有節點情況,可以按部就班的使用。講解一下此文中用到的註解。   @XmlRootElement 宣告xml的根節點。   @XmlType 使用propOrder指定xml的節點順序,同時可以指定namespace。   @XmlElement 宣告此欄位是xml中的節點欄位,以及name屬性
  @XmlAttribute 宣告此欄位是xml中的屬性值
  @XmlValue 宣告這是xml中帶屬性節點的值,例如 <class grade="primary">2</class> 這種節點
  @XmlElementWrapper 字面意思就是xml節點包裝。如果沒有此註解的話,我們有很多不便。得寫很
  的實體來滿足xml格式。   有不對的地方還請大家多多指教!