实体转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 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 getSpecialitys() {
		return specialitys;
	}


	public void setSpecialitys(List 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 specialitys = new ArrayList();
		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中带属性节点的值,例如 2 这种节点
  @XmlElementWrapper 字面意思就是xml节点包装。如果没有此注解的话,我们有很多不便。得写很
  的实体来满足xml格式。


  有不对的地方还请大家多多指教!

你可能感兴趣的:(java技术)