王芳,雷菊陽
(上海工程技術大學 機械學院,上海 201620)
網絡數據交換中的XML定時數據壓縮
王芳,雷菊陽
(上海工程技術大學 機械學院,上海 201620)
XML即可擴展標記語言,是為了在網絡中說明和交換數據而制定的標準數據格式,而作為數據載體的XML文檔的體積也隨著交換數據增多而變得越來越大, 這給網絡上的數據傳輸造成了很大的負擔。為此,先把數據庫里的數據生成XML,再加工成壓縮包,就是每一條數據都是xml.zip的壓縮包,利用C#編寫定時程序進行定時壓縮,不用手動,最后用于數據推送。
XML;數據壓縮;C#
隨著互聯網的飛速發(fā)展,網絡間的信息交換變得越來越頻繁, XML[1]為互聯網的數據交換而設計,它不僅僅是SGML[2]定義的用于描述的文檔,而且使電子商務等很多領域數據交換[2]成為可能。XML的文本表現手法、標記的符號化會導致XML數據比二進制表數據量增加,尤其當數據量很大的時候,效率就成為很大的問題。對文件進行壓縮[3],這樣可以使傳輸的數據變得更小,有利于基于數據的傳輸,而且要使XML壓縮技術作為底層壓縮工具來應用,就必須減少人工干預,進行定時數據壓縮。
可擴展標記語言 XML(Extensible Markup Language),是一種類似于超文本標記語言[4]的結構化標記語言,用來進行數據的存儲和傳輸,是目前最常用的數據表現和傳輸方式之一。不同于超文本標記語言,XML 的標簽是用戶自定義的,具有自我描述性[5],能夠清晰地向用戶或者應用程序展現其所包含的數據。
數據的優(yōu)點[6]在于:能應用于互聯網數據交換,由于存在各種格式的數據,且其具有數據自描述性和豐富的數據表達能力,使得各種數據之間可以無縫交換;它能實現異構異質系統間[7]的通信,傳統的結構化數據庫難以適應多系統異構數據的融合,而數據由于具有自我描述性,能很好地適應這種數據集成的需要,為電子商務的發(fā)展提供了優(yōu)越的條件。
C#[8]是一種安全的、穩(wěn)定的、簡單的、由C和C++衍生出來的面向對象的編程語言。它在繼承C和C++強大功能的同時去掉了一些復雜特性。C#綜合了VB簡單的可視化操作和C++的高運行效率,以其強大的操作能力、創(chuàng)新的語言特性和便捷性成為面向組件編程的首選語言。
2.1 數據庫連接程序
首先連接數據庫,即 從VS2008中連接SQL2008數據庫[9],程序如下:
private DBOperator DB=DBOperatorFactory.GetDBOperator(ConfigurationManager.Connectionstrings["CONNTEMP"].ConnectionString,ConfigurationManager.AppSettings["SQLSERVER"]);
System.Timers.Timer mytime=new System.Timers.Timer(ConfigurationManager.AppSettings["commandTimeout"]).ToInt32());
//Timer 10 s執(zhí)行一次
2.2 生成XML程序
XML的自描述性使其非常適用于不同應用間的數據交換,而且這種交換是不以預先規(guī)定一組數據結構定義為前提。在數據交換中使用,程序開發(fā)人員可以方便地用自定義的標記來描述數據。XML 作為一種中間件和數據承載工具, 在數據交換領域顯示了它強大的功能和方便的使用性。因此要將數據轉換為XML,程序如下:private string GetXml(string ID,string datatype,string datacode){
SqlParameterCollection spc=DB.CreateSqlParameterCollection();
Spc.Add("@ID",ID);
Spc.Add("@datatype",datatype);
Spc.Add("@datacode",datacode);
String sql=@"";
DataTable dt=DB.ExeSqlForDataTable(sql,spc,"dt");
String XmlString="select * from temp where ID=@ID and datatype=@datatype and datatype=@datatype";
foreach (DataRow dr in dt.Rows){
XmlString +=@"
XmlString +=@"
for (int i=0,i
XmlString+="<"+dt.Columns[i].ColumnName+">"+dr[dt.Columns[i].ColumnName]+""+dt.Columns[i].ColumnName+">";
If (dt.Columns[i].ColumnName=="VERSION")
}
XmlString+="";
XmlString+="";
}
if (dt.Columns[i].ColumnName=="DATA_TIMESTAMP")
}
2.3 定時程序
通過定時程序可以實現較少的人工干預,提供友好的用戶接口,要使XML壓縮技術作為底層壓縮工具來應用,就必須減少人工干預,因此編寫定時程序進行定時壓縮[10],程序如下:
Protected override void OnStart(string[] args){
//TODO:在此處添加代碼以啟動服務
mytime.Elapsed+=new ElapsedEventHandler(mytime_Elapsed);
mytime.Enabled=true;
mytime.AutoReset=true;
}
void mytime_Elapsed(object source,ElapsedEventArgs e){
String time=DataTime.Nov.ToString("HHmmss");
if (time.Equals(ConfigurationManager.AppSettings["Time"])){
WriteLog("開始");
mytime.Stop().
//先關掉計時器,防止還沒有處理完成再開啟一個 //線程出來
String DateforTran==DataTime.Nov.ToString("yyyy-mm-dd");//傳送數據的時間節(jié)點
TransDataLxxm(DateforTran);//數據信息
Mytime.Start();//執(zhí)行完再打開,防止再開啟一個 //線程出來
}
我國水利規(guī)劃近年總體進展及重點把握的幾個問題…………………………………… 吳 強,周智偉,郭東陽(9.27)
}
2.4 壓縮程序
由于數據壓縮是節(jié)省系統存儲空間和提高傳輸效率的有效途徑,通過對文件進行壓縮,可以使傳輸的數據變得更小,減少程序的執(zhí)行代價和執(zhí)行時間,有利于基于數據的傳輸,壓縮程序如下:
private void XmlTozip(string strXML,string datacode{
string strFolder="D:\TempFiles\";
if(!System.IO.Directory.Exists(strFolder){
System.IO.Directory.CreateDirectory(strFolder);
DirectoryInfo folder=new DirectoryInfo(strFolder);
String strData=DataTime.Nov.ToString("yyyymmdd");
Int tempLsh=0;
Foreach(FileInfo file in folder.GetFiles(strData+"*"+datacode+".zip")){
if(tempLsh } tempLsh++; string strFileName=strFolder+strData+"-"+tempLsh.ToString("000000")+".xml"; string filenameZip=strData+"-"+tempLsh.ToString("000000")+".xml"; string FolderfilenameZip=strFolder+strData+"-"+tempLsh.ToString("000000")+".zip"; FileStream fs=new System.IO.FileStream(strFileName,System.IO.FileMode.OpenOrCreate,System.IO.FileAccess ReadWrite); StreamWrite sv=new System.IO.StreamWrite(fs); Sv.WriteLine(strXML); Sv.Flush(); Sv.Close(); Fs.Close(); FastZip fz=new FastZip(); Fz.CreateZip(FolderfilenameZip,strFolder,true,filenameZip); 2.5 運行結果 自行模擬運行結果程序如圖1所示,數據壓縮實現。 圖1 運行結果 [1] 鄭黎曉,王成.XML模式推斷研究綜述[J].電子學報,2016,44(2):2-3. [2] 張屹峰.DOM4j解析技術探究[J].現代計算機,2011(2):2-3. [3] 曾春平,王超.編程從入門到精通[M].北京:北京希望電子出版社,2002:19-28. [4] M Girardot,N.Sundaresan.Millau:an encoding format for efficient representation and exchange of XML documents [J].Computer Networks,2000,33(1-6):430-469. [5] 鐘世明,邵銳.基于位置服務系統中XML數據流壓縮方法[J].武漢理工大學學報,2006:20-34. [6] 徐珊.XML與Web Services數據整合模型及其在教育考試信息系統中的應用[D].南昌:南昌大學,2014:43-50. [7] 王丹琛.物流信息系統中XML數據壓縮與傳輸安全性研究[D].成都:西南交通大學,2007:32-45. [8] 王小科.李繼業(yè)開發(fā)寶典[M].北京:機械工業(yè)出版社,2012:3-5. [9] 朱立才,黃津津.C#的數據庫編程技術[M].福州:福建教育出版社,2005:21-40. [10] 周月.基于XML的異構數據交換在校園網中的應用[D].北京:首都師范大學,2008:4-10. 王芳(碩士),主要研究方向為機器學習;雷菊陽(副教授),從事智能控制、模式識別及分布式系統研究及教學。 XML Timing Data Compression of Network Data Exchange Wang Fang,Lei Juyang (College of Mechanical Engineering,Shanghai University of Engineering Science,Shanghai 201620,China) XML(Extensible Markup Language) is the standard data format that is developed to represent and exchange data in the network.The volume of the XML document as a data carrier also becomes larger as the amount of data exchanged increasing,which places a great burden on the data transmission over the network. First of all,the data in the database is generated into XML format,and then is processed into a compressed package,that is to say,each data is xml.zip compressed packet,the C# is used to program for the preparation of compressed time,not by manual,and finally for data transmission. XML;data compression;C# TP273 A ?士然 2017-02-07)結 語