一、使用情景
某些时候,我们需要将类转换为xml字符串或者文件进行发送或操作,jdk本身便提供了这样的工具。JAXBContext。
二、使用
代码中注释的很清楚了,就不解释了。主要有两个类:PersonBO是要转换的载体类。Main是测试类。
PersonBO
package com.example.demo; import javax.xml.bind.annotation.*; import java.util.List; //定义将属性还是get/set映射到xml文件中。如果没有的话会报错,因为默认会既处理属性也处理get/set,会报错提示出现两遍。 @XmlAccessorType(value = XmlAccessType.FIELD) //xml的root节点的名称 @XmlRootElement(name = "person") //指定属性在xml文件中的顺序 @XmlType(propOrder = { "id", "realName", "nickName", "address", "phoneNum", "families" }) public class PersonBO { public PersonBO() {} public PersonBO(String desc, Integer id, String realName, String nickName, String address, String phoneNum, List<String> families) { this.desc = desc; this.id = id; this.realName = realName; this.nickName = nickName; this.address = address; this.phoneNum = phoneNum; this.families = families; } //根节点的属性,例如:<person desc="属性"></person> @XmlAttribute(name = "desc") private String desc; //xml中的子节点 @XmlElement(name = "real_name", required = true, defaultValue = "no_name") private String realName; @XmlElement(name = "id", required = true, defaultValue = "no_id") private Integer id; @XmlElement(name = "nick_name") private String nickName; @XmlElement(name = "address") private String address; @XmlElement(name = "phone_num", required = true) private String phoneNum; @XmlList @XmlElement(name = "family_num") private List<String> families; public String getDesc() { return desc; } public PersonBO setDesc(String desc) { this.desc = desc; return this; } public String getRealName() { return realName; } public PersonBO setRealName(String realName) { this.realName = realName; return this; } public Integer getId() { return id; } public PersonBO setId(Integer id) { this.id = id; return this; } public String getNickName() { return nickName; } public PersonBO setNickName(String nickName) { this.nickName = nickName; return this; } public String getAddress() { return address; } public PersonBO setAddress(String address) { this.address = address; return this; } public String getPhoneNum() { return phoneNum; } public PersonBO setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; return this; } public List<String> getFamilies() { return families; } public PersonBO setFamilies(List<String> families) { this.families = families; return this; } @Override public String toString() { return "PersonBO{" + "id=" + id + ", realName='" + realName + '\'' + ", nickName='" + nickName + '\'' + ", address='" + address + '\'' + ", phoneNum='" + phoneNum + '\'' + ", families=" + families + '}'; } }
Main
package com.example.demo; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import java.io.*; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @author kingboy--KingBoyWorld@163.com * @date 2017/11/12 下午2:42 * @desc 转换类. */ public class Main { public static void main(String[] args) { //准备一个实体数据 List<String> families = Stream.of("father", "mother", "sister").collect(Collectors.toList()); PersonBO personBO = new PersonBO("人", 1, "小金", "kingboy", "北京", "13288888888", families); //转换 String xml = beanToXml(personBO); System.out.println(xml); } /** * bean转换为xml,如果转换失败,返回空字符串 * @param object * @return */ public static String beanToXml(Object object) { try { //创建操作类 JAXBContext jax = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = jax.createMarshaller(); //解析 StringWriter sw = new StringWriter(); //格式化 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, sw); return sw.toString(); } catch (JAXBException e) { e.printStackTrace(); } return ""; } }
未经允许请勿转载:程序喵 » 使用JDK自带工具将类转换为xml