实体转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格式。


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

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值