推薦:詮釋XMLXML即可擴(kuò)展標(biāo)記語言(eXtensible Markup Language)。標(biāo)記是指計(jì)算機(jī)所能理解的信息符號,通過此種標(biāo)記,計(jì)算機(jī)之間可以處理包含各種信息的文章等。如何定義這些標(biāo)記,既可以選擇國際通用的標(biāo)
從XML中讀取數(shù)據(jù)到內(nèi)存的實(shí)例:
public clsSimuResultByOneGoods GetOneGoodsSimulationXML(string PathAndFileName) { clsSimuResultByOneGoods OneGoods = new clsSimuResultByOneGoods();//自己定義的一個(gè)類 Hashtable AllLocationResult = new Hashtable(); System.Xml.XmlTextReader r = new XmlTextReader(PathAndFileName); string LocationID = ""; DataTable LocationTable = null; while(r.Read()) { if(r.NodeType == XmlNodeType.Element) { switch(r.LocalName) { case "Result": OneGoods.GoodsCode = r.GetAttribute("GoodsCode"); OneGoods.From = Convert.ToDateTime(r.GetAttribute("FromDate")); OneGoods.To = Convert.ToDateTime(r.GetAttribute("ToDate")); break; case "Location": LocationID = r.GetAttribute("ID"); LocationTable = new DataTable(); LocationTable.Columns.Add("Date",typeof(DateTime)); LocationTable.Columns.Add("SafetyStock",typeof(decimal)); LocationTable.Columns.Add("ForecastDemand",typeof(decimal)); LocationTable.Columns.Add("FinalOutput",typeof(decimal)); LocationTable.Columns.Add("FinalInput",typeof(decimal)); LocationTable.Columns.Add("SimuStock",typeof(decimal)); LocationTable.Columns.Add("SimuStockTime",typeof(decimal)); LocationTable.Columns.Add("ImportWorkDay",typeof(bool)); LocationTable.Columns.Add("ImportWorkDay",typeof(bool)); break; case "Record": if(LocationTable != null) { DataRow dr = LocationTable.NewRow(); dr["Date"] = Convert.ToDateTime(r.GetAttribute("Date")); if(r.GetAttribute("SafetyStock") != null && r.GetAttribute("SafetyStock") != "") dr["SafetyStock"] = Convert.ToDecimal(r.GetAttribute("SafetyStock")); if(r.GetAttribute("ForecastDemand") != null && r.GetAttribute("ForecastDemand") != "") dr["ForecastDemand"] = Convert.ToDecimal(r.GetAttribute("ForecastDemand")); if(r.GetAttribute("FinalInput") != null && r.GetAttribute("FinalInput") != "") dr["FinalInput"] = Convert.ToDecimal(r.GetAttribute("FinalInput")); if(r.GetAttribute("FinalOutput") != null && r.GetAttribute("FinalOutput") != "") dr["FinalOutput"] = Convert.ToDecimal(r.GetAttribute("FinalOutput")); if(r.GetAttribute("SimuStock") != null && r.GetAttribute("SimuStock") != "") dr["SimuStock"] = Convert.ToDecimal(r.GetAttribute("SimuStock")); if(r.GetAttribute("SimuStockTime") != null && r.GetAttribute("SimuStockTime") != "") dr["SimuStockTime"] = Convert.ToDecimal(r.GetAttribute("SimuStockTime")); if(r.GetAttribute("ImportWorkDay") != null && r.GetAttribute("ImportWorkDay") != "") dr["ImportWorkDay"] = Convert.ToBoolean(r.GetAttribute("ImportWorkDay")); if(r.GetAttribute("ExportWorkDay") != null && r.GetAttribute("ExportWorkDay") != "") dr["ExportWorkDay"] = Convert.ToBoolean(r.GetAttribute("ExportWorkDay")); LocationTable.Rows.Add(dr);
} break; default: break; } } else if(r.NodeType == XmlNodeType.EndElement) { switch(r.LocalName) { case "Location": if(LocationTable != null) { LocationTable.AcceptChanges(); AllLocationResult.Add(LocationID,LocationTable); LocationID = ""; LocationTable = null; } break; default: break; } } } OneGoods.AllLocationResult = AllLocationResult; return OneGoods;
}
|
分享:怎樣利用XMLBean輕松讀寫XML一、關(guān)于XML解析
XML在Java應(yīng)用程序里變得越來越重要, 廣泛應(yīng)用于數(shù)據(jù)存儲和交換. 比如我們常見的配置文件,都是以XML方式存儲的. XML還應(yīng)用于Java Message Service和Web Services等技術(shù)作為數(shù)