【譯】修改大XML文件的有效方法
By Dare Obasanjo
Microsoft Corporation
April 2004
譯 by 烈火青春
2010-07-18
概述:Dare Obasanjo展示了兩種方法來(lái)更新或操作大XML文件,如日志文件、數據庫的dump文件。
介紹:隨著(zhù)XML越來(lái)越流行于作為儲存大量信息的帶格式文件,開(kāi)發(fā)者已經(jīng)意識到了編輯大XML文件的問(wèn)題了。對那些記錄著(zhù)日志文件并需要不斷追加信息到這些文件中的應用尤為突出。大多數簡(jiǎn)單的方法來(lái)編輯XML文件就是直接用XmlDocument來(lái)加載,然后在內存中編輯document對象,然后再保存回到磁盤(pán)中。然而這樣做意味著(zhù)整個(gè)XML都加載到了內存中,這可能會(huì )讓?xiě)贸绦蛘荚诖罅績(jì)却娑兊貌豢尚小?br>本文本展示了一些不需要用XmlDocument加載XML文件來(lái)來(lái)修改XML文件的方法。
使用XML包含的方法(Using XML Inclusion Techniques)
將要介紹的第一種方法非常適合于追加數據到XML日志文件中。開(kāi)發(fā)者常面對的一個(gè)問(wèn)題是需要簡(jiǎn)單地追加一些新實(shí)體到一個(gè)日志文件,而不是先加載整個(gè)document。因為XML有著(zhù)規則的結構,所以用傳統方式將一個(gè)實(shí)體追加到文件的結尾是基本不可行的。
這個(gè)方法將會(huì )展示的是能夠快速在XML document中插入一個(gè)對象。該方法需要產(chǎn)生兩個(gè)文件,第一個(gè)是XML結構文件,而第二個(gè)文件是一個(gè)XML數據片段。XML結構文件包含包著(zhù)XML片段的定義,采用外部定義實(shí)體的方式DTD或使用xi:include element。采用這種包含XML片段的文件,就可以簡(jiǎn)單地直接追加文件到XML中。以下是一個(gè)采用XML定義與XML片段分離的例子:
Logfile.xml:
<?xml version="1.0"?>
<!DOCTYPE logfile [
<!ENTITY events
SYSTEM "logfile-entries.txt">
]>
<logfile>
&events;
</logfile>
Logfile-events.txt:
<event>
<ip>127.0.0.1</ip>
<http_method>GET</http_method>
<file>index.html</file>
<date>2004-04-01T17:35:20.0656808-08:00</date>
</event>
<event>
<ip>127.0.0.1</ip>
<http_method>GET</http_method>
<file>stylesheet.css</file>
<date>2004-04-01T17:35:23.0656120-08:00</date>
<referrer>http://www.example.com/index.html</referrer>
</event>
<event>
<ip>127.0.0.1</ip>
<http_method>GET</http_method>
<file>logo.gif</file>
<date>2004-04-01T17:35:25.238220-08:00</date>
<referrer>http://www.example.com/index.html</referrer>
</event>
這個(gè)logfile-entires.txt文件包含著(zhù)XML片段數據,可以有效地使用典型的文件IO操作函數。下面的代碼展示著(zhù)一個(gè)實(shí)體是如何追加到這個(gè)XML日志文件中:
using System;
using System.IO;
using System.Xml;
public class Test{
public static void Main(string[] args){
StreamWriter sw = File.AppendText("logfile-entries.txt");
XmlTextWriter xtw = new XmlTextWriter(sw);
xtw.WriteStartElement("event");
xtw.WriteElementString("ip", "192.168.0.1");
xtw.WriteElementString("http_method", "POST");
xtw.WriteElementString("file", "comments.aspx");
xtw.WriteElementString("date", "1999-05-05T19:25:13.238220-08:00");
xtw.Close();
}
}
一旦實(shí)體追加到這個(gè)txt文件中后,通過(guò)傳統地XML處理方式就可以讀到這個(gè)數據。下面的代碼采用Xpath來(lái)遍歷logfile.xml中的所有events,列表展示出所有訪(fǎng)問(wèn)過(guò)的頁(yè)面信息。
using System;
using System.Xml;
public class Test2{
public static void Main(string[] args){
XmlValidatingReader vr =
new XmlValidatingReader(new XmlTextReader("logfile.xml"));
vr.ValidationType = ValidationType.None;
vr.EntityHandling = EntityHandling.ExpandEntities;
XmlDocument doc = new XmlDocument();
doc.Load(vr);
foreach(XmlElement element in doc.SelectNodes("http://event")){
string file = element.ChildNodes[2].InnerText;
string date = element.ChildNodes[3].InnerText;
Console.WriteLine("{0} accessed at {1}", file, date);
}
}
}
這段代碼的輸出是:
index.html accessed at 2004-04-01T17:35:20.0656808-08:00
stylesheet.css accessed at 2004-04-01T17:35:23.0656120-08:00
logo.gif accessed at 2004-04-01T17:35:25.238220-08:00
comments.aspx accessed at 1999-05-05T19:25:13.238220-08:00
Chaining an XmlReader to an XmlWriter
某些情況下可能需要對XML文件進(jìn)行更細致的操作此外還有僅需要在根結點(diǎn)插入一個(gè)元素的操作。例如,可能在日志文件存檔時(shí)需要將不滿(mǎn)足某些規則的元素篩選出來(lái)。一般的做法是加載XML到XmlDocument中,然后將通過(guò)Xpath將這些規則下的元素選擇出來(lái)。然后,這樣做需要將整個(gè)XML加到內存中,如果文檔太大程序可能會(huì )崩潰。另一個(gè)想到是利用XSLT來(lái)解決,但同樣這樣也會(huì )面臨XmlDocument加載所有到內存中的問(wèn)題。當然,開(kāi)發(fā)人員可能還不熟悉XSLT的話(huà),要理解如何用模板匹配屬性跨度就有點(diǎn)大了。
一個(gè)解決這類(lèi)問(wèn)題的方法就是如何用XmlReader讀取XML文件,加以處理處理大XML文件,然后再用XmlWriter寫(xiě)出已經(jīng)讀出的內容。這種方法不會(huì )一次性將整個(gè)文件加載到內存中,在簡(jiǎn)單地追加寫(xiě)文件前可以做各種操作。下面的代碼就展示著(zhù),從前面讀到XML文件,然后保存XML文件前,排除IP=“127.0.0.1”的元素數據。(代碼可以粘帖至控制臺程序運行來(lái)查看結果?。?br>using System;
using System.Xml;
using System.IO;
using System.Text;
public class Test2{
static string ipKey;
static string httpMethodKey;
static string fileKey;
static string dateKey;
static string referrerKey;
public static void WriteAttributes(XmlReader reader, XmlWriter writer){
if(reader.MoveToFirstAttribute()){
do{
writer.WriteAttributeString(reader.Prefix,
reader.LocalName,
reader.NamespaceURI,
reader.Value);
}while(reader.MoveToNextAttribute());
reader.MoveToElement();
}
}
public static void WriteEvent(XmlWriter writer, string ip, string httpMethod, string file, string date, string referrer){
writer.WriteStartElement("event");
writer.WriteElementString("ip", ip);
writer.WriteElementString("http_method", httpMethod);
writer.WriteElementString("file", file);
writer.WriteElementString("date", date);
if(referrer != null)
writer.WriteElementString("referrer", referrer);
writer.WriteEndElement();
}
public static void ReadEvent(XmlReader reader, out string ip, out string httpMethod, out string file, out string date, out string referrer){
ip = httpMethod = file = date = referrer = null;
while( reader.Read() && reader.NodeType != XmlNodeType.EndElement){
if (reader.NodeType == XmlNodeType.Element) {
if(reader.Name == ipKey){
ip = reader.ReadString();
}else if(reader.Name == httpMethodKey){
httpMethod = reader.ReadString();
}else if(reader.Name == fileKey){
file = reader.ReadString();
}else if(reader.Name == dateKey){
date = reader.ReadString();
// reader.Read(); // consume end tag
}else if(reader.Name == referrerKey){
referrer = reader.ReadString();
}
}//if
}//while
}
public static void Main(string[] args){
string ip, httpMethod, file, date, referrer;
//setup XmlNameTable with strings we'll be using for comparisons
XmlNameTable xnt = new NameTable();
ipKey = xnt.Add("ip");
httpMethodKey = xnt.Add("http_method");
fileKey = xnt.Add("file");
dateKey = xnt.Add("date");
referrerKey = xnt.Add("referrer");
//load XmlTextReader using XmlNameTable above
XmlTextReader xr = new XmlTextReader("logfile.xml", xnt);
xr.WhitespaceHandling = WhitespaceHandling.Significant;
XmlValidatingReader vr = new XmlValidatingReader(xr);
vr.ValidationType = ValidationType.None;
vr.EntityHandling = EntityHandling.ExpandEntities;
StreamWriter sw =
new StreamWriter ("logfile-archive.xml", false, Encoding.UTF8 );
XmlWriter xw = new XmlTextWriter (sw);
vr.MoveToContent(); // Move to document element
xw.WriteStartElement(vr.Prefix, vr.LocalName, vr.NamespaceURI);
WriteAttributes(vr, xw);
vr.Read(); // Move to first <event> child of document element
// Write out each event that isn't from 127.0.0.1 (localhost)
do
{
ReadEvent(vr, out ip, out httpMethod,
out file, out date, out referrer);
if(!ip.Equals("127.0.0.1")){
WriteEvent(xw,ip, httpMethod, file, date, referrer);
}
vr.Read(); //move to next <event> element or end tag of <logfile>
} while(vr.NodeType == XmlNodeType.Element);
Console.WriteLine("Done");
vr.Close();
xw.Close();
}
}
上面的代碼將會(huì )輸出到一個(gè)logfile-archive.xml文件中:
<logfile>
<event>
<ip>192.168.0.1</ip>
<http_method>POST</http_method>
<file>comments.aspx</file>
<date>1999-05-05T19:25:13.238220-08:00</date>
</event>
</logfile>
在上面代碼中除了使用XmlReader來(lái)傳到XmlWriter外,還使用了NameTable來(lái)提升從ReadEvent()方法中取取元素的名稱(chēng)比較的性能。這種使用法在XMLReader中的好處已經(jīng)在MSDN中的Object Comparison Using XmlNameTable with XmlReader一文中指出。