从数据库中生成XML文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从数据库中生成XML文件相关的知识,希望对你有一定的参考价值。
前台页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" language="javascript"> function CheckFileType() { var objButton = document.getElementById("uploadFile");//选择文件控件 var uploadSubBtn = document.getElementById("uploadSubBtn");//提交按钮 var FileName = new String(objButton.value);//文件名 var extension = new String(FileName.substring( FileName.lastIndexOf(".") + 1, FileName.length));//文件扩展名 if (extension == "xls")//你可以添加扩展名 { uploadSubBtn.disabled = false;//启用上传按钮 } else { alert("您选择的不是excel文件,请选择excel文件!"); uploadSubBtn.disabled = true;//禁用上传按钮 objButton.outerHTML = objButton.outerHTML; //这样清空,在IE8下也能执行成功 objButton.select(); document.selection.clear(); //好像这种方法也可以清空 input file 的value值,不过我没测试 } } function CheckAuditedFileType() { var objButton = document.getElementById("uploadAuditedFile");//选择文件控件 var uploadSubBtn = document.getElementById("uploadAuditedBtn");//提交按钮 var FileName = new String(objButton.value);//文件名 var extension = new String(FileName.substring( FileName.lastIndexOf(".") + 1, FileName.length));//文件扩展名 if (extension == "xml")//你可以添加扩展名 { uploadSubBtn.disabled = false;//启用上传按钮 } else { alert("您选择的不是xml文件,请选择xml文件!"); uploadSubBtn.disabled = true;//禁用上传按钮 objButton.outerHTML = objButton.outerHTML; //这样清空,在IE8下也能执行成功 objButton.select(); document.selection.clear(); //好像这种方法也可以清空 input file 的value值,不过我没测试 } } </script> <title>Insert title here</title> </head> <body> <form id="uploadFileForm" action="../upload/uploadSaleData" method="post" enctype="multipart/form-data"> <input id="uploadFile" type="file" name="file" onchange="javascript:CheckFileType()" /> <input id="uploadSubBtn" type="submit" disabled="disabled" value="上传" /> </form> <br /> <form id="disposeFlightDataForm" action="../upload/disposeFlightData" method="post"> <input id="disposeFlightDataButton" type="submit" value="处理航班数据" /> </form> <br /> <form id="importSalesDataForm" action="../upload/importSalesData" method="post" enctype="multipart/form-data"> <input id="importSalesData" type="submit" value="导入销售数据" /> </form> <br /> <br /> <h1>引擎:</h1> <form id="testXmlForm" action="../upload/testCreateXml" method="post" enctype="multipart/form-data"> <input id="createXML" type="submit" value="生成引擎XML" /> </form> <br /> <br /> <form id="uploadAuditedFileForm" action="../upload/uploadAuditedXML" method="post" enctype="multipart/form-data"> <input id="uploadAuditedFile" type="file" name="file" onchange="javascript:CheckAuditedFileType()" /> <input id="uploadAuditedBtn" type="submit" disabled="disabled" value="上传引擎审核结果文件" /> </form> </body> </html>
后台代码
@Controller
@RequestMapping(value = "/upload")
public class UploadSaleDataAction类
1 @Autowired 2 private StoreEngineResult engineResult; 3 4 @RequestMapping(value = "/testCreateXml") 5 public String testCreateXml() { 6 engine.createEngineInputFile(); 7 return GO_UPLAODEXCEL; 8 }
@JobTypeAnnotation(value = "CREATE_ENGINE_INPUT_INTERFACE", contextType = ContextType.ParallelJobContext)
@Component
public class CreateEngineInputInterface类
createEngineInputFile()方法
1 /** 2 * 生成引擎输入接口文件 3 */ 4 public void createEngineInputFile() { 5 // 根据是否送引擎标识(C:无需送;N:待送;Y:已送;)查询Sal_Tkt表的字段ToEngineInd=‘N‘的票 6 List<Ticket> tickets = ticketService.findTicketByToEngineInd("M"); 7 log.info("共有" + tickets.size() + "张需要送引擎的票"); 8 if (tickets != null && tickets.size() > 0) { 9 long start = System.currentTimeMillis(); 10 11 // 创建文档的根节点 12 Element root = DocumentHelper.createElement("TRANSACTIONS"); 13 Document document = DocumentHelper.createDocument(root); 14 15 for (Ticket ticket : tickets) { 16 Element transaction = root.addElement("TRANSACTION"); 17 18 String saleType = ticket.getTicketBasicSale().getSaleType(); 19 if ("N".equals(saleType)) { 20 CreateNormalTicketElement(ticket, transaction); 21 } 22 23 if ("E".equals(saleType)) { 24 List<OldCoupon> oldRefundResissueCoupon = CreateExchangeTicketElement(false, 25 ticket, transaction); 26 27 /* 前一张oldTickets */ 28 Ticket oldTicket = ticketService.findTicketByTktNo(oldRefundResissueCoupon.get( 29 0).getTktNo()); 30 31 if (oldTicket != null) { 32 Element former = transaction.addElement("FORMER"); 33 34 String saleTypeE = oldTicket.getTicketBasicSale().getSaleType(); 35 if ("N".equals(saleTypeE)) { 36 CreateNormalTicketElement(oldTicket, former); 37 } 38 39 if ("E".equals(saleTypeE)) { 40 CreateExchangeTicketElement(true, oldTicket, former); 41 } 42 } 43 } 44 45 if ("R".equals(saleType)) { 46 List<OldCoupon> oldRefundResissueCoupons = CreateRefundTicketElement(ticket, 47 transaction); 48 49 Set<String> oldTktNos = new HashSet<String>(); 50 for (OldCoupon oldCoupon : oldRefundResissueCoupons) { 51 oldTktNos.add(oldCoupon.getTktNo()); 52 } 53 54 Element former = null; 55 if (oldTktNos.size() > 0) { 56 former = transaction.addElement("FORMER"); 57 } 58 59 /* 前一张oldTickets */ 60 /* 退票获取旧票 */ 61 String saleTypeR = ""; 62 while (oldTktNos.size() > 0) { 63 Ticket oTicket = ticketService.findTicketByTktNo(oldTktNos.iterator() 64 .next()); 65 // 删除集合中的票号 66 oldTktNos.remove(oldTktNos.iterator().next()); 67 if (oTicket != null) { 68 saleTypeR = oTicket.getTicketBasicSale().getSaleType(); 69 if ("N".equals(saleTypeR)) { 70 CreateNormalTicketElement(oTicket, former); 71 } 72 73 if ("E".equals(saleTypeR)) { 74 CreateExchangeTicketElement(true, oTicket, former); 75 } 76 77 // 删除联票票号 78 List<ConjunctionTicket> cnjs = oTicket.getConjunctionTickets(); 79 if (cnjs != null) { 80 for (ConjunctionTicket conjunctionTicket : cnjs) { 81 if (oldTktNos.contains(conjunctionTicket.getTktNo())) { 82 oldTktNos.remove(conjunctionTicket.getTktNo()); 83 } 84 } 85 } 86 } 87 } 88 } 89 } 90 91 try { 92 CreateXmlUtil.writeXml(document); 93 } catch (Exception e) { 94 log.error("创建文件失败!" + e.getMessage()); 95 } 96 long end = System.currentTimeMillis(); 97 log.info("生成文件共用时:"+ (end - start)); 98 } 99 }
CreateNormalTicketElement方法
正常票
private void CreateNormalTicketElement(Ticket ticket, Element transaction) { NormalTicket normalTicket = (NormalTicket) ticket; List<FciSector> fciSectors = normalTicket.getFciSectors(); List<CouponTax> couponTaxs = normalTicket.getCouponTaxs(); List<Tax> ticketTaxs = normalTicket.getTaxs(); List<FciFareComponent> fareComponets = normalTicket.getFciFareComponents(); CreateXmlUtil.GenerateTicketElement(transaction, normalTicket, fciSectors, couponTaxs, ticketTaxs, fareComponets, null); }
换票
CreateXmlUtil类
CreateExchangeTicketElement
public static final String UPLOAD_DIR = "/usr/app/saleData";
private List<OldCoupon> CreateExchangeTicketElement(boolean isOld, Ticket ticket, Element transaction) { ExchangeTicket exchangeTicket = (ExchangeTicket) ticket; List<FciSector> fciSectors = exchangeTicket.getFciSectors(); List<CouponTax> couponTaxs = exchangeTicket.getCouponTaxs(); List<Tax> ticketTaxs = exchangeTicket.getTaxs(); List<FciFareComponent> fareComponets = exchangeTicket.getFciFareComponents(); List<OldCoupon> oldRefundResissueCoupon = null; if (!isOld) { oldRefundResissueCoupon = exchangeTicket.getOldCoupons(); } CreateXmlUtil.GenerateTicketElement(transaction, exchangeTicket, fciSectors, couponTaxs, ticketTaxs, fareComponets, oldRefundResissueCoupon); return oldRefundResissueCoupon; }
添加需要导出的xml元素节点
public static Element GenerateTicketElement(Element transaction, Ticket ticketObj, List<FciSector> fciSectors, List<CouponTax> couponTaxs, List<Tax> ticketTaxs, List<FciFareComponent> fareComponets, List<OldCoupon> oldCoupons) { Element ticket = transaction.addElement("TICKET"); // ticket属性赋值 GenerateTicketAttributeElement(ticket, ticketObj); // 给TICKET节点添加COUPON子节点 if (fciSectors != null) { for (FciSector fciSector : fciSectors) { GenerateSectorElement(ticket, fciSector); } } // 给TICKET节点添加FARECOMPONET子节点 if (fareComponets != null) { for (FciFareComponent fareComponent : fareComponets) { GenerateFareComponetElement(ticket, fareComponent); } } // 给TICKET节点添加TICKETTAX子节点 if (ticketTaxs != null) { for (Tax ticketTax : ticketTaxs) { GenerateTicketTaxElement(ticket, ticketTax); } } // 给TICKET节点添加COUPONTAX子节点 if (couponTaxs != null) { for (CouponTax couponTax : couponTaxs) { GenerateCouponTaxElement(ticket, couponTax); } } // 给TICKET节点添加OLDREFUNDRESISSUECOUPON子节点 if (oldCoupons != null) { for (OldCoupon oldCoupon : oldCoupons) { GenerateOldCouponElement(ticket, oldCoupon); } } return transaction; }
其他元素的节点添加依次类推
/** * // 给TICKET节点添加FARECOMPONET子节点 * * @param ticket */ private static void GenerateFareComponetElement(Element ticket, FciFareComponent fareComponent) { Element fareComponetObj = ticket.addElement("FARECOMPONET"); fareComponetObj.addElement("AT_NO").addText( fareComponent.getAtNo() == null ? "" : fareComponent.getAtNo().toString()); fareComponetObj.addElement("FARE_BASIS").addText( fareComponent.getFareBasis() == null ? "" : fareComponent.getFareBasis()); fareComponetObj.addElement("AT_AMT").addText( fareComponent.getAtAmt() == null ? "" : fareComponent.getAtAmt().toString()); fareComponetObj.addElement("SC_IND").addText( fareComponent.getScInd() == null ? "" : fareComponent.getScInd()); fareComponetObj.addElement("EMS_NUM").addText( fareComponent.getEmsNum() == null ? "" : fareComponent.getEmsNum()); fareComponetObj.addElement("CONSTRUCT_CITY").addText( fareComponent.getConstructCity() == null ? "" : fareComponent.getConstructCity()); fareComponetObj.addElement("UPL_CITY").addText( fareComponent.getUplCity() == null ? "" : fareComponent.getUplCity()); fareComponetObj.addElement("DIS_CITY").addText( fareComponent.getDisCity() == null ? "" : fareComponent.getDisCity()); }
将添加完的xml元素添加到需要写入的元素中
/** * 写xml * * @param document * @throws UnsupportedEncodingException * @throws FileNotFoundException * @throws IOException */ public static void writeXml(Document document) throws UnsupportedEncodingException, FileNotFoundException, IOException { // 把生成的xml文档存放在硬盘上 true代表是否换行 OutputFormat format = new OutputFormat(" ", true); format.setEncoding("UTF-8");// 设置编码格式 XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(UPLOAD_DIR + File.separator + "transaction.xml"), format); // XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("E:/usr/app/saleData/transaction.xml"), format); xmlWriter.write(document); xmlWriter.close(); }
生成的xml 文件
<?xml version="1.0" encoding="UTF-8"?> <TRANSACTIONS> <TRANSACTION> <TICKET> <TKT_NO>7818507521899</TKT_NO> <AIRLINE_CODE>MU</AIRLINE_CODE> <SALE_TYPE>N</SALE_TYPE> <SALE_SOURCE>ATK</SALE_SOURCE> <AGENT_NO>08677349</AGENT_NO> <AGENT_NAME>东航商务网站(SHA777)国内DB2C</AGENT_NAME> <AGENT_COUNTRY>CN</AGENT_COUNTRY> <ET_TYPE>C</ET_TYPE> <ISSUE_DATE>2015-08-30</ISSUE_DATE> <REPORT_CUR>CNY</REPORT_CUR> <RMT_AMT>880</RMT_AMT> <PGN_TYPE>ADT</PGN_TYPE> <PNR_NO>ND8Q54</PNR_NO> <ENDORSEMENT>不得签转</ENDORSEMENT> <TOUR_CODE>WEB15002</TOUR_CODE> <FCMI_IND>0</FCMI_IND> <STATION>SHA</STATION> <ISSUE_POINT>SHA</ISSUE_POINT> <SALE_COUNTRY>CN</SALE_COUNTRY> <GROUP_TKT_IND>N</GROUP_TKT_IND> <DI_IND>D</DI_IND> <FREE_IND>N</FREE_IND> <CNJ_IND>Y</CNJ_IND> <CNJ_TKT_NO>7818507521899</CNJ_TKT_NO> <CNJ_SEQ>1</CNJ_SEQ> <CNJ_COUNT>2</CNJ_COUNT> <OAI_IND></OAI_IND> <CHANGE_FEE>0</CHANGE_FEE> <FLIGHT_MISS_FEE>0</FLIGHT_MISS_FEE> <FOP1>CA</FOP1> <FOP2>CC</FOP2> <FOP3>CA</FOP3> <FOP4>CC</FOP4> <ISSUE_TIME>150010</ISSUE_TIME> <GOV_IND>N</GOV_IND> <GOV_DIVISION>G</GOV_DIVISION> <TO_FARE_AUDIT_IND>Y</TO_FARE_AUDIT_IND> <TO_COMM_AUDIT_IND>Y</TO_COMM_AUDIT_IND> <TO_TAX_AUDIT_IND>Y</TO_TAX_AUDIT_IND> <ACC_FARE_SALE_AMT>10</ACC_FARE_SALE_AMT> <FARE_SALE_AMT>830</FARE_SALE_AMT> <TAX_SALE_AMT>50</TAX_SALE_AMT> <COMM_RATE_SALE_AMT>0</COMM_RATE_SALE_AMT> <COMM_SALE_AMT>0</COMM_SALE_AMT> <TRAVEL_START_DATE>2015-10-10</TRAVEL_START_DATE> < VAT_SALE_AMT>10</ VAT_SALE_AMT> < INTERLINE_IND>Y</ INTERLINE_IND> <SECTOR> <CPN_NO>1</CPN_NO> <TKT_NO>7818507521899</TKT_NO> <IT_SEQ>1</IT_SEQ> <AT_NO>1</AT_NO> <STOPOVER_IND>X</STOPOVER_IND> <UPL_AIRPORT>PEK</UPL_AIRPORT> <DIS_AIRPORT>KMG</DIS_AIRPORT> <UPL_CITY>BJS</UPL_CITY> <DIS_CITY>KMG</DIS_CITY> <UPL_COUNTRY>CN</UPL_COUNTRY> <DIS_COUNTRY>CN</DIS_COUNTRY> <MARKET_CARRIER>MU</MARKET_CARRIER> <OPERATE_CARRIER>MU</OPERATE_CARRIER> <MARKET_FLIGHT_NO>5708</MARKET_FLIGHT_NO> <OPERATE_FLIGHT_NO>5708</OPERATE_FLIGHT_NO> <TICKET_CLASS>V</TICKET_CLASS> <FARE_BASIS>VXS</FARE_BASIS> <CABIN_CLASS>Y</CABIN_CLASS> <FLIGHT_DATE>2015-09-01</FLIGHT_DATE> <DATE_IND>N</DATE_IND> <NVBD>2015-10-10</NVBD> <NVAD>2015-10-10</NVAD> <FLT_DEPT_TIME>2035</FLT_DEPT_TIME> <FLT_ARR_TIME>2035</FLT_ARR_TIME> <FREE_XBG>5</FREE_XBG> <FARE_SALE_AMT>15</FARE_SALE_AMT> <FARE_BREAK>N</FARE_BREAK> <FC_CARRIER>MU</FC_CARRIER> <TAX_SALE_AMT>53</TAX_SALE_AMT> <USED_IND>N</USED_IND> <COUP_STATUS>F</COUP_STATUS> <ACTUAL_FLIGHT_DATE>2015-10-05</ACTUAL_FLIGHT_DATE> <COUP_TYPE>T</COUP_TYPE> <BA_IND>T</BA_IND> <REFUND_TRAVEL_TIME_DIF>10</REFUND_TRAVEL_TIME_DIF> <PNR_CANCEL_DATE>2015-10-01</PNR_CANCEL_DATE> </SECTOR> <FARECOMPONET> <AT_NO>1</AT_NO> <FARE_BASIS>T</FARE_BASIS> <AT_AMT>450</AT_AMT> <SC_IND>Y</SC_IND> <EMS_NUM>10</EMS_NUM> <CONSTRUCT_CITY>SHA</CONSTRUCT_CITY> <UPL_CITY>XIY</UPL_CITY> <DIS_CITY>HGH</DIS_CITY> </FARECOMPONET> <TICKETTAX> <TKT_NO>7818507521899</TKT_NO> <TAX_CODE>CN</TAX_CODE> <XF_CODE>001</XF_CODE> <TAX_SALE_AMT>50</TAX_SALE_AMT> <PUBLISH_CUR>CNY</PUBLISH_CUR> <PUBLISH_AMT>0</PUBLISH_AMT> <PD_IND>N</PD_IND> <TAX_FEE_IND>T</TAX_FEE_IND> <REPORT_CUR>CNY</REPORT_CUR> </TICKETTAX> <TICKETTAX> <TKT_NO>7818507521899</TKT_NO> <TAX_CODE>YQ</TAX_CODE> <XF_CODE></XF_CODE> <TAX_SALE_AMT>0</TAX_SALE_AMT> <PUBLISH_CUR></PUBLISH_CUR> <PUBLISH_AMT>0</PUBLISH_AMT> <PD_IND></PD_IND> <TAX_FEE_IND>T</TAX_FEE_IND> <REPORT_CUR>CNY</REPORT_CUR> </TICKETTAX> <TICKETTAX> <TKT_NO>7818507521899</TKT_NO> <TAX_CODE>OB</TAX_CODE> <XF_CODE></XF_CODE> <TAX_SALE_AMT>0</TAX_SALE_AMT> <PUBLISH_CUR></PUBLISH_CUR> <PUBLISH_AMT>0</PUBLISH_AMT> <PD_IND></PD_IND> <TAX_FEE_IND>S</TAX_FEE_IND> <REPORT_CUR>CNY</REPORT_CUR> </TICKETTAX> <TICKETTAX> <TKT_NO>7818507521899</TKT_NO> <TAX_CODE>OC</TAX_CODE> <XF_CODE></XF_CODE> <TAX_SALE_AMT>0</TAX_SALE_AMT> <PUBLISH_CUR></PUBLISH_CUR>以上是关于从数据库中生成XML文件的主要内容,如果未能解决你的问题,请参考以下文章