JAXB-java对象转XML字符串
2021-07-20 15:05
标签:static app tom repr this orm string row ext public class Ben2XmlUtil { /** //自定义命名空间前缀 public static void main(String[] args) throws JAXBException, FileNotFoundException { CEB622Message customer = new CEB622Message(); //将java对象转换为XML字符串 //自定義命名空間以及後綴 继承了 NamespacePrefixMapper JAXBContext context = JAXBContext.newInstance(customer.getClass()); } } package cst.goodsList.entity; import java.util.ArrayList; import javax.xml.bind.annotation.*; @XmlRootElement(name = "CEB622Message") private List @XmlAttribute public String getGuid() { public void setGuid(String guid) { public CEB622Message() { public List public void setInventoryReturn(List package cst.goodsList.entity; public class book { private String guid ="AA"; ------------------------------------------------------代码完毕--------------------------------------------------------- JAXB-java对象转XML字符串 标签:static app tom repr this orm string row ext 原文地址:https://www.cnblogs.com/sunqingwei/p/9517512.html工具類
* 将对象直接转换成String类型的 XML输出
*
* @param obj
* @return
*/
public static String convertToXml(Object obj) {
// 创建输出流
StringWriter sw = new StringWriter();
try {
// 利用jdk中自带的转换类实现
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
// 格式化xml输出的格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// 将对象转换成输出流形式的xml
marshaller.marshal(obj, sw);
} catch (JAXBException e) {
e.printStackTrace();
}
return sw.toString();
}
public static class PreferredMapper extends NamespacePrefixMapper {
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
return "os2";
}
}
book books = new book();
customer.getInventoryReturn().add(books);
String xmlStr = convertToXml(customer);
System.out.println(xmlStr);
Marshaller m = context.createMarshaller();
NamespacePrefixMapper mapper = new PreferredMapper();
m.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(customer, System.out);实体
import java.util.List;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "name")
public class CEB622Message {
private String guid = "123";
return guid;
}
this.guid = guid;
}
InventoryReturn = new ArrayList();
}
return InventoryReturn;
}
InventoryReturn = inventoryReturn;
}
}实体二//放入真正字段
private String customsCode ="BB";
private String ebpCode = "CC";
private String ebcCode = "DD";
private String agentCode ="EE";
private String copNo ="FF";
private String preNo ="GG";
private String returnStatus= "HH";
private String returnTime= "JJ";
private String returnInfo="QQ";
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public String getCustomsCode() {
return customsCode;
}
public void setCustomsCode(String customsCode) {
this.customsCode = customsCode;
}
public String getEbpCode() {
return ebpCode;
}
public void setEbpCode(String ebpCode) {
this.ebpCode = ebpCode;
}
public String getEbcCode() {
return ebcCode;
}
public void setEbcCode(String ebcCode) {
this.ebcCode = ebcCode;
}
public String getAgentCode() {
return agentCode;
}
public void setAgentCode(String agentCode) {
this.agentCode = agentCode;
}
public String getCopNo() {
return copNo;
}
public void setCopNo(String copNo) {
this.copNo = copNo;
}
public String getPreNo() {
return preNo;
}
public void setPreNo(String preNo) {
this.preNo = preNo;
}
public String getReturnStatus() {
return returnStatus;
}
public void setReturnStatus(String returnStatus) {
this.returnStatus = returnStatus;
}
public String getReturnTime() {
return returnTime;
}
public void setReturnTime(String returnTime) {
this.returnTime = returnTime;
}
public String getReturnInfo() {
return returnInfo;
}
public void setReturnInfo(String returnInfo) {
this.returnInfo = returnInfo;
}
}反序列话效果
EE