工作的时候遇到一个任务,需要将sql查出的数据转成xml格式的数据进行传输。
这里用的structs2框架,dao查询出来的数据是ResultSet类型数据转成List< Map >类型数据,然后把每一个字段数据取出来去null处理,再写入xml中
import java.io.File;
import java.io.FileOutputStream;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
public String createXml(String appNo){
try {
// 1、创建document对象
Document document = DocumentHelper.createDocument();
// 2、创建根节点rootElement
Element rootElement = document.addElement("DOCUMENT");
// 4、生成子节点及子节点内容
Element item = rootElement.addElement("ITEM");
ResultSet appInfoRs = dao.getAppInfoByAppNo(appNo);
List<Map> appInfoMapList = DaoUtil.rsToMapList(appInfoRs);
if(appInfoMapList.size()>0){
Map appInfoItem = appInfoMapList.get(0);
Element orgNo = item.addElement("ORG_NO");
orgNo.setText(killNull(appInfoItem.get("org_no")+""));
Element orgName = item.addElement("ORG_NAME");
orgName.setText(killNull(appInfoItem.get("org_name")+""));
Element gcNo = item.addElement("GC_NO");
gcNo.setText(killNull(appInfoItem.get("gc_no")+""));
Element gcName = item.addElement("GC_NAME");
gcName.setText(killNull(appInfoItem.get("gc_name")+""));
Element voltCode = item.addElement("VOLT_CODE");
voltCode.setText(killNull(appInfoItem.get("volt_code")+""));
Element absoMode = item.addElement("ABSO_MODE");
absoMode.setText(killNull(appInfoItem.get("abso_mode")+""));
Element cenGovSub = item.addElement("CEN_GOV_SUB");
cenGovSub.setText(killNull(appInfoItem.get("cen_gov_sub")+""));
Element billType = item.addElement("BILL_TYPE");
billType.setText(killNull(appInfoItem.get("bill_type")+""));
Element taxRate = item.addElement("TAX_RATE");
taxRate.setText(killNull(appInfoItem.get("tax_rate")+""));
Element buildDate = item.addElement("BUILD_DATE");
buildDate.setText(killNull(appInfoItem.get("build_date")+""));
Element gcDate = item.addElement("GC_DATE");
gcDate.setText(killNull(appInfoItem.get("gc_date")+""));
Element cancelDate = item.addElement("CANCEL_DATE");
cancelDate.setText(killNull(appInfoItem.get("cancel_date")+""));
Element gcAddr = item.addElement("GC_ADDR");
gcAddr.setText(killNull(appInfoItem.get("gc_addr")+""));
Element gcType = item.addElement("GC_TYPE");
gcType.setText(killNull(appInfoItem.get("gc_type")+""));
Element acctName = item.addElement("ACCT_NAME");
acctName.setText(killNull(appInfoItem.get("acct_name")+""));
Element bankCode = item.addElement("BANK_CODE");
bankCode.setText(killNull(appInfoItem.get("bank_code")+""));
Element bankAcct = item.addElement("BANK_ACCT");
bankAcct.setText(killNull(appInfoItem.get("bank_acct")+""));
Element statusCode = item.addElement("STATUS_CODE");
statusCode.setText(killNull(appInfoItem.get("status_code")+""));
Element contractCap = item.addElement("CONTRACT_CAP");
contractCap.setText(killNull(appInfoItem.get("contract_cap")+""));
Element contractNo = item.addElement("CONTRACT_NO");
contractNo.setText(killNull(appInfoItem.get("contract_no")+""));
Element appTypeCode = item.addElement("APP_TYPE_CODE");
appTypeCode.setText(killNull(appInfoItem.get("app_type_code")+""));
}
else{
Element orgNo = item.addElement("ORG_NO");
orgNo.setText("");
Element orgName = item.addElement("ORG_NAME");
orgName.setText("");
Element gcNo = item.addElement("GC_NO");
gcNo.setText("");
Element gcName = item.addElement("GC_NAME");
gcName.setText("");
Element voltCode = item.addElement("VOLT_CODE");
voltCode.setText("");
Element absoMode = item.addElement("ABSO_MODE");
absoMode.setText("");
Element cenGovSub = item.addElement("CEN_GOV_SUB");
cenGovSub.setText("");
Element billType = item.addElement("BILL_TYPE");
billType.setText("");
Element taxRate = item.addElement("TAX_RATE");
taxRate.setText("");
Element buildDate = item.addElement("BUILD_DATE");
buildDate.setText("");
Element gcDate = item.addElement("GC_DATE");
gcDate.setText("");
Element cancelDate = item.addElement("CANCEL_DATE");
cancelDate.setText("");
Element gcAddr = item.addElement("GC_ADDR");
gcAddr.setText("");
Element gcType = item.addElement("GC_TYPE");
gcType.setText("");
Element acctName = item.addElement("ACCT_NAME");
acctName.setText("");
Element bankCode = item.addElement("BANK_CODE");
bankCode.setText("");
Element bankAcct = item.addElement("BANK_ACCT");
bankAcct.setText("");
Element statusCode = item.addElement("STATUS_CODE");
statusCode.setText("");
Element contractCap = item.addElement("CONTRACT_CAP");
contractCap.setText("");
Element contractNo = item.addElement("CONTRACT_NO");
contractNo.setText("");
Element appTypeCode = item.addElement("APP_TYPE_CODE");
appTypeCode.setText("");
}
Element gcTypeCodeRoot = item.addElement("GCTYPECODE");
Element gcTypeRoot = gcTypeCodeRoot.addElement("GCTYPE");
ResultSet typeCodeRootRs = dao.getAppInfoByAppNo(appNo);
List<Map> typeCodeRootMapList = DaoUtil.rsToMapList(typeCodeRootRs);
if(typeCodeRootMapList.size()>0){
for(int i=0;i<typeCodeRootMapList.size();i++){
Map typeCodeRootMapItem = typeCodeRootMapList.get(i);
Element gcTypeCode = gcTypeRoot.addElement("GC_TYPE_CODE");
gcTypeCode.setText(killNull(typeCodeRootMapItem.get("gc_type_code")+""));
}
}
else{
Element gcTypeCode = gcTypeRoot.addElement("GC_TYPE_CODE");
gcTypeCode.setText("");
}
Element fcProInfoRoot = item.addElement("FCPROINFO");
ResultSet proInfoRootRs = dao.getProInfoByAppNo (appNo); //项目信息
List<Map> proInfoRootMapList = DaoUtil.rsToMapList(proInfoRootRs);
if(proInfoRootMapList.size()>0){
for(int j=0;j<proInfoRootMapList.size();j++){
Map proInfoMapItem = proInfoRootMapList.get(j);
Element proInfoRoot = fcProInfoRoot.addElement("PROINFO");
Element approveLevel = proInfoRoot.addElement("APPROVE_LEVEL");
approveLevel.setText(killNull(proInfoMapItem.get("approve_level")+""));
Element proAmoMon = proInfoRoot.addElement("PRO_AMO_MON");
proAmoMon.setText(killNull(proInfoMapItem.get("pro_amo_mon")+""));
Element proAppDoc = proInfoRoot.addElement("APPROVE_LEVEL");
proAppDoc.setText(killNull(proInfoMapItem.get("approve_level")+""));
Element proAppTime = proInfoRoot.addElement("PRO_APP_TIME");
proAppTime.setText(killNull(proInfoMapItem.get("pro_app_time")+""));
}
}
else{
Element proInfoRoot = fcProInfoRoot.addElement("PROINFO");
Element approveLevel = proInfoRoot.addElement("APPROVE_LEVEL");
approveLevel.setText("");
Element proAmoMon = proInfoRoot.addElement("PRO_AMO_MON");
proAmoMon.setText("");
Element proAppDoc = proInfoRoot.addElement("APPROVE_LEVEL");
proAppDoc.setText("");
Element proAppTime = proInfoRoot.addElement("PRO_APP_TIME");
proAppTime.setText("");
}
Element fcContactRoot = item.addElement("FCCONTACT");
ResultSet fcContactRootRs = dao.getUserInfoByAppNo (appNo); //项目信息
List<Map> fcContactRootMapList = DaoUtil.rsToMapList(fcContactRootRs);
if(fcContactRootMapList.size()>0){
for(int k=0;k<fcContactRootMapList.size();k++){
Map fcContactRootMapItem = fcContactRootMapList.get(k);
Element contact = fcContactRoot.addElement("CONTACT");
Element contactMode = contact.addElement("CONTACT_MODE");
contactMode.setText(killNull(fcContactRootMapItem.get("contact_mode")+""));
Element contactName = contact.addElement("CONTACT_NAME");
contactName.setText(killNull(fcContactRootMapItem.get("contact_name")+""));
Element officeTel = contact.addElement("OFFICE_TEL");
officeTel.setText(killNull(fcContactRootMapItem.get("office_tel")+""));
Element addr = contact.addElement("ADDR");
addr.setText(killNull(fcContactRootMapItem.get("addr")+""));
}
}
else{
Element contact = fcContactRoot.addElement("CONTACT");
Element contactMode = contact.addElement("CONTACT_MODE");
contactMode.setText("");
Element contactName = contact.addElement("CONTACT_NAME");
contactName.setText("");
Element officeTel = contact.addElement("OFFICE_TEL");
officeTel.setText("");
Element addr = contact.addElement("ADDR");
addr.setText("");
}
Element fcCertRoot = item.addElement("FCCERT");
ResultSet fcCertRootRs = dao.getCertInfoByAppNo (appNo); //项目信息
List<Map> fcCertRootMapList = DaoUtil.rsToMapList(fcCertRootRs);
if(fcCertRootMapList.size()>0){
for(int l=0;l<fcCertRootMapList.size();l++){
Map fcCertRootMapItem = fcCertRootMapList.get(l);
Element cert = fcCertRoot.addElement("CERT");
Element certTypeCode = cert.addElement("CERT_TYPE_CODE"); certTypeCode.setText(killNull(fcCertRootMapItem.get("cert_type_code")+""));
Element certName = cert.addElement("CERT_NAME");
certName.setText(killNull(fcCertRootMapItem.get("cert_name")+""));
Element certNo = cert.addElement("CERT_NO");
certNo.setText(killNull(fcCertRootMapItem.get("cert_no")+""));
}
}
else{
Element cert = fcCertRoot.addElement("CERT");
Element certTypeCode = cert.addElement("CERT_TYPE_CODE");
certTypeCode.setText("");
Element certName = cert.addElement("CERT_NAME");
certName.setText("");
Element certNo = cert.addElement("CERT_NO");
certNo.setText("");
}
// 5、设置生成xml的格式
OutputFormat format = OutputFormat.createPrettyPrint();
// 设置编码格式
format.setEncoding("UTF-8");
System.out.println("生成sg_send_FC_DocInfo.xml成功");
System.out.println(document.asXML().toString());
System.out.println("打印每一行来查看效果");
Element el=document.getRootElement();
Iterator it=el.elementIterator();
while (it.hasNext()) {
Element element=(Element) it.next();
System.out.println(element.getName());
}
//转成xml样式的字符串
return document.asXML().toString();
// // 6、生成xml文件
// File file = new File("sg_send_FC_DocInfo.xml");
// XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
// // 设置是否转义,默认使用转义字符
// writer.setEscapeText(false);
// writer.write(document);
// writer.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("生成sg_send_FC_DocInfo.xml失败");
return null;
}
}
/**
* Rs取出的数据去null处理
* @param str
* @return
*/
public String killNull(String str){
if("null".equals(str) || "".equals(str)){
return "";
}
else{
return str;
}
}
生成的XMl字符串结果如下
<DOCUMENT>
<ITEM>
<ORG_NO>3440102</ORG_NO>
<ORG_NAME>城北供电公司</ORG_NAME>
<GC_NO>3115618157</GC_NO>
<GC_NAME>测试一下</GC_NAME>
<VOLT_CODE>AC00061</VOLT_CODE>
<ABSO_MODE>03</ABSO_MODE>
<CEN_GOV_SUB>99</CEN_GOV_SUB>
<BILL_TYPE>02</BILL_TYPE>
<TAX_RATE>0</TAX_RATE>
<BUILD_DATE>2020-06-10 15:41:12.0</BUILD_DATE>
<GC_DATE>2020-06-10 15:41:12.0</GC_DATE>
<CANCEL_DATE></CANCEL_DATE>
<GC_ADDR>浙江省杭州市西湖区追痕测试街道追痕测试居委会追痕测试道路追痕测试小区</GC_ADDR>
<GC_TYPE>03</GC_TYPE>
<ACCT_NAME>测试一下</ACCT_NAME>
<BANK_CODE>A102</BANK_CODE>
<BANK_ACCT>6666888</BANK_ACCT>
<STATUS_CODE>1</STATUS_CODE>
<CONTRACT_CAP>1000</CONTRACT_CAP>
<CONTRACT_NO></CONTRACT_NO>
<APP_TYPE_CODE></APP_TYPE_CODE>
<GCTYPECODE>
<GCTYPE>
<GC_TYPE_CODE></GC_TYPE_CODE>
</GCTYPE>
</GCTYPECODE>
<FCPROINFO>
<PROINFO>
<APPROVE_LEVEL></APPROVE_LEVEL>
<PRO_AMO_MON></PRO_AMO_MON>
<APPROVE_LEVEL></APPROVE_LEVEL>
<PRO_APP_TIME></PRO_APP_TIME>
</PROINFO>
</FCPROINFO>
<FCCONTACT>
<CONTACT>
<CONTACT_MODE>02</CONTACT_MODE>
<CONTACT_NAME>测试</CONTACT_NAME>
<OFFICE_TEL>021-66668888</OFFICE_TEL>
<ADDR></ADDR>
</CONTACT>
</FCCONTACT>
<FCCERT>
<CERT>
<CERT_TYPE_CODE>02</CERT_TYPE_CODE>
<CERT_NAME>测试一下</CERT_NAME>
<CERT_NO>55556666</CERT_NO>
</CERT>
<CERT>
<CERT_TYPE_CODE>03</CERT_TYPE_CODE>
<CERT_NAME>测试一下2</CERT_NAME>
<CERT_NO>555566667777</CERT_NO>
</CERT>
</FCCERT>
</ITEM>
</DOCUMENT>
这里的一些标签使支持循环的,例如证件信息< cert ></ cert>查出来是几条数据就应该显示几条数据