标签:static app tom repr this orm string row ext
public class Ben2XmlUtil {
/**
* 将对象直接转换成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";
}
}
public static void main(String[] args) throws JAXBException, FileNotFoundException {
CEB622Message customer = new CEB622Message();
book books = new book();
customer.getInventoryReturn().add(books);
//将java对象转换为XML字符串
String xmlStr = convertToXml(customer);
System.out.println(xmlStr);
//自定義命名空間以及後綴 继承了 NamespacePrefixMapper
JAXBContext context = JAXBContext.newInstance(customer.getClass());
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);
}
}
package cst.goodsList.entity;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "CEB622Message")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "name")
public class CEB622Message {
private List<book> InventoryReturn;
@XmlAttribute
private String guid = "123";
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public CEB622Message() {
InventoryReturn = new ArrayList<>();
}
public List<book> getInventoryReturn() {
return InventoryReturn;
}
public void setInventoryReturn(List<book> inventoryReturn) {
InventoryReturn = inventoryReturn;
}
}
package cst.goodsList.entity;
public class book {
private String guid ="AA";
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;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<os2:CEB622Message os2:xmlns="com.sun.xml.bind.namespacePrefixMapper" guid="123">
<InventoryReturn>
<agentCode>EE</agentCode>
<copNo>FF</copNo>
<customsCode>BB</customsCode>
<ebcCode>DD</ebcCode>
<ebpCode>CC</ebpCode>
<guid>AA</guid>
<preNo>GG</preNo>
<returnInfo>QQ</returnInfo>
<returnStatus>HH</returnStatus>
<returnTime>JJ</returnTime>
</InventoryReturn>
<os2:/CEB622Message>
------------------------------------------------------代码完毕---------------------------------------------------------
标签:static app tom repr this orm string row ext
原文地址:https://www.cnblogs.com/sunqingwei/p/9517512.html