Xml Entity 实体互转(XStream)
http://x-stream.github.io/tutorial.html
项目中用了 JAXB 方式 Xml Entity 实体互转(JAXB)
实体:
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamConverter; import com.thoughtworks.xstream.converters.basic.BooleanConverter; import java.io.Serializable; import java.util.Date; /** * 患者基本信息 */ @XStreamAlias("PatientInfo") public class PatientInfoVo implements Serializable { /** * 患者ID */ private String patientId; /** * 患者信息 */ @XStreamAlias("PAT_NAME") private String patientName; /** * 患者性别 */ @XStreamAlias("PatientGender") @XStreamConverter(value= BooleanConverter.class,booleans={false},strings={"男","女"}) private Boolean gender; /** * 出生日期 */ @XStreamAlias("DATE_BIRTH") private Date patientBirthdate; /** * */ @XStreamAlias("MedicalType") //@XStreamConverter(value= EnumToStringConverter.class,ints={1},strings={"门诊","急诊","住院"}) //TODO 待解决 private Integer medicalType; public String getPatientId() { return patientId; } public void setPatientId(String patientId) { this.patientId = patientId; } public String getPatientName() { return patientName; } public void setPatientName(String patientName) { this.patientName = patientName; } public Date getPatientBirthdate() { return patientBirthdate; } public void setPatientBirthdate(Date patientBirthdate) { this.patientBirthdate = patientBirthdate; } public Boolean getGender() { return gender; } public void setGender(Boolean gender) { this.gender = gender; } public Integer getMedicalType() { return medicalType; } public void setMedicalType(Integer medicalType) { this.medicalType = medicalType; } @Override public String toString() { return "PatientInfoVo{" + "patientId='" + patientId + '\'' + ", patientName='" + patientName + '\'' + ", gender=" + gender + ", patientBirthdate=" + patientBirthdate + ", medicalType=" + medicalType + '}'; } }
Test代码
import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.converters.basic.DateConverter; import com.thoughtworks.xstream.io.xml.DomDriver; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import java.util.Date; import java.util.TimeZone; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class XmlTests { Logger logger = LoggerFactory.getLogger(this.getClass()); /** * 玩得不熟,没有拼字符串来得直接 */ @Test void entityToxmlTest() { PatientInfoVo info = new PatientInfoVo(); info.setPatientId("这个没添加注解"); info.setPatientName("张三"); info.setGender(false); info.setMedicalType(1); //TODO 待转成应对的汉字 info.setPatientBirthdate(new Date()); //将实体转成XML XStream xStream = new XStream(new DomDriver()); //使用注解转换 xStream.processAnnotations(PatientInfoVo.class); //xstream.registerConverter(); //时间格式化后输出,默认没有 yyyy-MM-dd HH:mm:ss,注意时区也要调整 xStream.registerConverter(new DateConverter("yyyy-MM-dd HH:mm:ss", new String[]{"yyyy-MM-dd HH:mm:ss"}, TimeZone.getTimeZone("GMT+8"))); xStream.alias("Patient", PatientInfoVo.class); String xml = xStream.toXML(info); logger.info("\r\n" + xml); } /** * 反转还行,挺方便,复杂的结构可以参考一些其它资料 */ @Test void xmlToEntityTest() { String xml = "<PatientInfo>\n" + " <patientId>这个没添加注解</patientId>\n" + " <PAT__NAME>张三</PAT__NAME>\n" + " <PatientGender>女</PatientGender>\n" + " <DATE__BIRTH>2021-10-22 10:13:40</DATE__BIRTH>\n" + " <MedicalType>1</MedicalType>\n" + "</PatientInfo>"; //将实体转成XML XStream xStream = new XStream(new DomDriver()); //使用注解转换 xStream.processAnnotations(PatientInfoVo.class); //默认的不支持 yyyy-MM-dd HH:mm:ss 需要格式化一下 xStream.registerConverter(new DateConverter("yyyy-MM-dd HH:mm:ss", new String[]{"yyyy-MM-dd HH:mm:ss"}, TimeZone.getTimeZone("GMT+8"))); PatientInfoVo user = (PatientInfoVo) xStream.fromXML(xml); logger.info(user.toString()); } }
本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/15437790.html