网上搜到很多方式创建xml文件,个人觉得这种结果最清晰直接,代码如下:
package com.foxera.util;
import java.io.IOException;
import java.io.StringWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import com.foxera.viewmodels.RequestinfoNew;
public class CreateXmlstring {
public String createXML(RequestinfoNew requestinfo, String totalPersonCost,
String totalSoftwareCost, String totalMaintainCost) {
String strXML = null;
Document document = DocumentHelper.createDocument();
Element root = document.addElement("FEEMASTER");
Element billno = root.addElement("BILLNO");// ϵ½y×Ô„ÓŽ§³ö
Element budgettype = root.addElement("BUDGETTYPE");// ¹Ì¶¨žé3
budgettype.addText("3");
Element askdate = root.addElement("ASKDATE");// ϵ½y×Ô„ÓŽ§³ö
Element companyid = root.addElement("COMPANYID");// ÐèÇó†ÎλµÄ·¨ÈËID
companyid.addText(requestinfo.getLegalEntityId());
Element askdept = root.addElement("ASKDEPT");// ×Ô„Ó®aÉú£¬¿Éžé¿Õ
askdept.addText(requestinfo.getReqCostCode()); // ¸ù“þÐèÇóááíµÄ¸ü¸Ä£¬ÌîÖµžéÙMÓôú´a
Element askuser = root.addElement("ASKUSER");// ×Ô„Ó®aÉú£¬¿Éžé¿Õ
Element currcode = root.addElement("CURRCODE");// RMB£¬Ó††ÎÖеĎńe™ÚλÌṩ
currcode.addText(requestinfo.getCurrency());
Element askphone = root.addElement("ASKPHONE");// ×Ô„Ó®aÉú£¬¿Éžé¿Õ
Element signtype = root.addElement("SIGNTYPE");// ¹Ì¶¨žé2
signtype.addText("2");
Element createuser = root.addElement("CREATEUSER");// ¹¤ºÅ ÓÉÓ††ÎÖеÄÉêÕˆÈË™ÚλÌṩ
createuser.addText(requestinfo.getJobnum());
Element remark = root.addElement("REMARK");// ¿Éžé¿Õ
Element feetype = root.addElement("FEETYPE");// ¹Ì¶¨žé1
feetype.addText("1");
Element flag = root.addElement("FLAG");// ¹Ì¶¨žé3£»20180314±ä¸üΪ¹Ì¶¨Öµ1
flag.addText("1");
Element signflag = root.addElement("SIGNFLAG");// ¹Ì¶¨žé3£»20180314±ä¸üΪ¿Õ
signflag.addText("");
Element fromsys = root.addElement("FORMSYS");// ¹Ì¶¨ÖµPDSS£»20180322 ÐÂÔö×Ö¶Î
fromsys.addText("PDSS");
// ¶©µ¥ÀàÐÍΪ¿ª·¢
if (requestinfo.getSalesOrderType() == 1) {
// ÈËÁ¦·ÑÓñí PersonCost
Element items1 = root.addElement("ITEMS");
Element item1 = items1.addElement("ITEM");
item1.addText("1");
Element acccode = items1.addElement("ACCCODE");// ÙMÓÿÆÄ¿´ú´a
// £¬žéÓ††ÎµÄ³ÐÀ¿†ÎλÙMÓÿÆÄ¿´ú´aÌṩ
acccode.addText(requestinfo.getContractUnitAccord());
Element expensetype = items1.addElement("EXPENSETYPE");// ¹Ì¶¨žéC
expensetype.addText("C");
// žéÓ††ÎµÄ¿‚ÙMÓã¬ÈôÓ††ÎžéC1é_°l£¬„t헴΄tžéÈËÁ¦ÙMÓÿ‚ÙMÓü°Ü›ówŒ§Èë¿‚ÙMÓã¬ÈôÓ††ÎžéC2é_°l£¬„t헴Ξé¾S×o¿‚ÙMÓ㬴ËÌŽÐèßMÐнðî~µÄ¼Ó¿‚ÙMÓÃ
Element applymount = items1.addElement("APPLYAMOUNT");
applymount.addText(totalPersonCost);
Element remark2 = items1.addElement("REMARK");// ½»Ò×ÕfÃ÷
remark2.addText("œyÔ‡ÓÃ");
Element costcode = items1.addElement("COSTCODE");// ÙMÓôú´a,Ϊӆ†ÎµÄÐèÇó†ÎλÙMÓôú´a
costcode.addText(requestinfo.getReqCostCode());
Element vencode = items1.addElement("VENCODE");// SÉÌ´ú´a£¬žé³Ð”ˆ†ÎλµÄSÉÌ´ú´a
//½ØÈ¡vencodeÇ°ÃæµÄCodeµÄÖµ
String vencodeNew="";
if(requestinfo.getContractUnitVencode().length()>0 && requestinfo.getContractUnitVencode().indexOf("_")>0){
vencodeNew=requestinfo.getContractUnitVencode().substring(0, requestinfo.getContractUnitVencode().indexOf("_"));
}
vencode.addText(vencodeNew);
Element items2 = root.addElement("ITEMS");
Element item2 = items2.addElement("ITEM");
item2.addText("2");
Element acccode2 = items2.addElement("ACCCODE");// ÙMÓÿÆÄ¿´ú´a
acccode2.addText(requestinfo.getContractUnitAccord());
Element expensetype2 = items2.addElement("EXPENSETYPE");// ¹Ì¶¨žéC
expensetype2.addText("C");
Element applymount2 = items2.addElement("APPLYAMOUNT");
applymount2.addText(totalSoftwareCost);
Element remark3 = items2.addElement("REMARK");
remark3.addText("œyÔ‡ÓÃ");
Element costcode2 = items2.addElement("COSTCODE");
costcode2.addText(requestinfo.getReqCostCode());
Element vencode2 = items2.addElement("VENCODE");
vencode2.addText(requestinfo.getContractUnitVencode());
}
// ¶©µ¥ÀàÐÍΪά»¤
if (requestinfo.getSalesOrderType() == 2) {
// ά»¤·ÑÓñí MaintainCost
Element items1 = root.addElement("ITEMS");
Element item1 = items1.addElement("ITEM");
item1.addText("1");
Element acccode = items1.addElement("ACCCODE");// ÙMÓÿÆÄ¿´ú´a
acccode.addText(requestinfo.getContractUnitAccord());
Element expensetype = items1.addElement("EXPENSETYPE");// ¹Ì¶¨žéC
expensetype.addText("C");
Element applymount = items1.addElement("APPLYAMOUNT");
applymount.addText(totalMaintainCost);
Element remark2 = items1.addElement("REMARK");
remark2.addText("œyÔ‡ÓÃ");
Element costcode = items1.addElement("COSTCODE");
costcode.addText(requestinfo.getReqCostCode());
Element vencode = items1.addElement("VENCODE");
//vencode.addText(requestinfo.getContractUnitVencode());
//½ØÈ¡vencodeÇ°ÃæµÄCodeµÄÖµ
String vencodeNew="";
if(requestinfo.getContractUnitVencode().length()>0 && requestinfo.getContractUnitVencode().indexOf("_")>0){
vencodeNew=requestinfo.getContractUnitVencode().substring(0, requestinfo.getContractUnitVencode().indexOf("_"));
}
vencode.addText(vencodeNew);
}
// --------
StringWriter strWtr = new StringWriter();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter xmlWriter = new XMLWriter(strWtr, format);
try {
xmlWriter.write(document);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
strXML = strWtr.toString();
return strXML;
}
}
maven 添加依赖
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.1</version>
</dependency>