<?xml version="1.0" encoding="utf-8"?>
<ceb:CEB311Message guid="4CDE1CFD-EDED-46B1-946C-B8022E42FC94" version="1.0" xmlns:ceb="http://www.chinaport.gov.cn/ceb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ceb:Order>
<ceb:OrderHead>
<ceb:guid>${id}</ceb:guid>
<ceb:appType>1</ceb:appType>
<ceb:appTime>20160308112701</ceb:appTime>
<ceb:appStatus>2</ceb:appStatus>
<ceb:orderType>I</ceb:orderType>
<ceb:orderNo>order20160321116421002</ceb:orderNo>
<ceb:ebpCode>1105910159</ceb:ebpCode>
<ceb:ebpName>${name}</ceb:ebpName>
<ceb:ebcCode>1105910159</ceb:ebcCode>
<ceb:ebcName>东方物通科技(北京)有限公司</ceb:ebcName>
<ceb:goodsValue>14000</ceb:goodsValue>
<ceb:freight>5000</ceb:freight>
<ceb:discount>0</ceb:discount>
<ceb:taxTotal>50</ceb:taxTotal>
<ceb:acturalPaid>19050</ceb:acturalPaid>
<ceb:currency>142</ceb:currency>
<ceb:buyerRegNo>ID20160001</ceb:buyerRegNo>
<ceb:buyerName>aa</ceb:buyerName>
<ceb:buyerIdType>1</ceb:buyerIdType>
<ceb:buyerIdNumber>130681136250023332</ceb:buyerIdNumber>
<ceb:payCode>1105910159</ceb:payCode>
<ceb:payName>东方物通科技(北京)有限公司</ceb:payName>
<ceb:payTransactionId>20160001634226001</ceb:payTransactionId>
<ceb:batchNumbers>20160317</ceb:batchNumbers>
<ceb:consignee>焦洪宇</ceb:consignee>
<ceb:consigneeTelephone>13522652231</ceb:consigneeTelephone>
<ceb:consigneeAddress>北京市海淀区</ceb:consigneeAddress>
<ceb:consigneeDistrict>072750</ceb:consigneeDistrict>
<ceb:note>test</ceb:note>
</ceb:OrderHead>
<ceb:OrderList>
<ceb:gnum>1</ceb:gnum>
<ceb:itemNo>AF001-001</ceb:itemNo>
<ceb:itemName>b</ceb:itemName>
<ceb:itemDescribe>v</ceb:itemDescribe>
<ceb:barCode>2345123</ceb:barCode>
<ceb:unit>007</ceb:unit>
<ceb:qty>100</ceb:qty>
<ceb:price>20</ceb:price>
<ceb:totalPrice>2000</ceb:totalPrice>
<ceb:currency>142</ceb:currency>
<ceb:country>116</ceb:country>
<ceb:note></ceb:note>
</ceb:OrderList>
</ceb:Order>
</ceb:CEB311Message>
后台代码部分
@GetMapping(value = "/generator")
public void generator() {
XMLModel xmlModel = new XMLModel();
xmlModel.setId(UUID.randomUUID().toString().replaceAll("-", ""));
xmlModel.setName("张三");
// xmlModel.setDemoList(list);
// xml2XmlDoc(xmlModel, "D:\\123pri\\template.xml", "D:\\123pri\\newTemplate.xml");
// xml2XmlDoc(xmlModel, "E:\\11鹿港汇\\project\\java\\JiuBang-business\\JiuBang-order\\src\\main\\resources\\template\\template.xml", "D:\\123pri\\newTemplate.xml");
System.out.println(System.getProperty("user.dir"));
String s = System.getProperty("user.dir");
xml2XmlDoc(xmlModel, s + "\\JiuBang-business\\JiuBang-order\\src\\main\\resources\\template\\template.xml", "D:\\123pri\\newTemplate.xml");
System.out.println("send xml success!!!");
}
/**
* 将xml模板转换为newxml
*
* @param model 需要填充到模板的数据
* @param templetFilePath 模板文件路径
* @param targetFilePath 目标文件保存路径
*/
public static void xml2XmlDoc(XMLModel model, String templetFilePath, String targetFilePath) {
Writer out = null;
try {
// 将模板文件路径拆分为文件夹路径和文件名称
String tempLetDir = templetFilePath.substring(0, templetFilePath.lastIndexOf("\\"));
// 注意:templetFilePath.lastIndexOf("/")中,有的文件分隔符为:\ 要注意文件路径的分隔符
String templetName = templetFilePath.substring(templetFilePath.lastIndexOf("\\") + 1);
// 将目标文件保存路径拆分为文件夹路径和文件名称
String targetDir = targetFilePath.substring(0, targetFilePath.lastIndexOf("\\"));
String targetName = targetFilePath.substring(targetFilePath.lastIndexOf("\\") + 1);
Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
configuration.setDefaultEncoding(String.valueOf(StandardCharsets.UTF_8));
// 如果目标文件目录不存在创建
File file = new File(targetDir);
if (!file.exists()) {
file.mkdirs();
}
//加载模板数据(从文件路径中获取文件)
configuration.setDirectoryForTemplateLoading(new File(tempLetDir));
// configuration.setDirectoryForTemplateLoading(new File(tempLetDir));
//获取模板实例
Template template = configuration.getTemplate(templetName);
File outFile = new File(targetDir + File.separator + targetName);
//模板和数据模型合并生成文件
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), StandardCharsets.UTF_8));
//生成文件
template.process(model, out);
out.flush();
out.close();
} catch (Exception e) {
log.error("write xml failed:", e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.error("close out failed:", e);
}
}
}
}