Delphi7 支持對XML文檔的操作,可以通過TXMLDocument類來實現(xiàn)對XML文檔的讀寫??梢岳肨XMLDocument把XML文檔讀到內(nèi)存中,從而可以進行編輯、保存操作。TXMLDocument類是通過DOM(文檔對象模型)接口來訪問XML文檔中的各個元素的。對于DOM接口的實現(xiàn)有多種方式,Delphi支持的方式有:
1)微軟的MSXML SDK,這種方式是通過COM對象來實現(xiàn);
2) Apache 的Xerces的實現(xiàn)方式;
3)另外一種是開源OpenXML實現(xiàn)方式。對于不同的接口實現(xiàn)方式可以通過設(shè)定TXMLDocument的DOMVender來進行控制。
支持XML的Delphi單元主要存在與…\Borland\Delphi7\Source\Xml目錄下,主要包括:XMLIntf,XMLDoc,xmldom,msxmldom,xercesxmldom,xdom,oxmldom等單元。
·XMLIntf――包括了Borland自己定義的XML文檔的接口;
·XMLDoc――是對XMLIntf中所定義接口的Borland實現(xiàn);
·Xmldom――定義了DOM(文檔對象模型)接口,這里對DOM接口進行了Borland的實現(xiàn);
·Msxmldom――實現(xiàn)微軟對Xmldom中定義的接口的實現(xiàn),主要調(diào)用微軟的COM對象來實現(xiàn),對Xmldom中定義接口的封裝;
·Xercesxmldom――Borland通過Xerces XML DOM方式來實現(xiàn)對Xmldom中定義接口的封裝; l Oxmldom――Borland通過使用OpenXML來實現(xiàn)對Xmldom中定義接口的封裝;
TXMLDocument類的屬性,請參考Borland的幫助文件;
讀寫XML文檔
·讀取XML文檔
通常情況下不通過直接使用TXMLDocument對象來進行XML文件的讀取,而是使用XMLDoc單元中提供的幾個有用的函數(shù)來讀取XML文檔,這些函數(shù)包括:
function LoadXMLDocument(const FileName: DOMString): IXMLDocument;
function LoadXMLData(const XMLData: DOMString): IXMLDocument; overload;
function LoadXMLData(const XMLData: string): IXMLDocument; overload;
function NewXMLDocument(Version: DOMString = '1.0'): IXMLDocument;
可以看出這些函數(shù)全部返回的是IXMLDocument接口,得到了IXMLDocument接口在進行文檔的操作;這些函數(shù)都是通過創(chuàng)建TXMLDocument對象來實現(xiàn)對XML文檔的讀取的;其中NewXMLDocument僅僅創(chuàng)建一個IXMLDocument接口。
可以這樣利用NewXMLDocument來讀取XML文檔:
XMLDoc := NewXMLDocument;
XMLDoc.LoadFromFile(FileName);
·保存XML文檔
可以通過下面的方式來保存XML文檔:
XMLDoc := NewXMLDocument;
iRoot := IXMLDoc.CreateNode(‘TestXMLDocument’);
XMLDoc.DocumentElement := iRoot;
…
XMLDoc.SaveToFile(FileName);
可以看出通過接口來操作XML文檔是非常方便的;
選用不同類型的XML解析方式
1)微軟的MSXML SDK,這種方式是通過COM對象來實現(xiàn);
2) Apache 的Xerces的實現(xiàn)方式;
3)另外一種是開源OpenXML實現(xiàn)方式。對于不同的接口實現(xiàn)方式可以通過設(shè)定TXMLDocument的DOMVender來進行控制。
支持XML的Delphi單元主要存在與…\Borland\Delphi7\Source\Xml目錄下,主要包括:XMLIntf,XMLDoc,xmldom,msxmldom,xercesxmldom,xdom,oxmldom等單元。
·XMLIntf――包括了Borland自己定義的XML文檔的接口;
·XMLDoc――是對XMLIntf中所定義接口的Borland實現(xiàn);
·Xmldom――定義了DOM(文檔對象模型)接口,這里對DOM接口進行了Borland的實現(xiàn);
·Msxmldom――實現(xiàn)微軟對Xmldom中定義的接口的實現(xiàn),主要調(diào)用微軟的COM對象來實現(xiàn),對Xmldom中定義接口的封裝;
·Xercesxmldom――Borland通過Xerces XML DOM方式來實現(xiàn)對Xmldom中定義接口的封裝; l Oxmldom――Borland通過使用OpenXML來實現(xiàn)對Xmldom中定義接口的封裝;
TXMLDocument類的屬性,請參考Borland的幫助文件;
讀寫XML文檔
·讀取XML文檔
通常情況下不通過直接使用TXMLDocument對象來進行XML文件的讀取,而是使用XMLDoc單元中提供的幾個有用的函數(shù)來讀取XML文檔,這些函數(shù)包括:
function LoadXMLDocument(const FileName: DOMString): IXMLDocument;
function LoadXMLData(const XMLData: DOMString): IXMLDocument; overload;
function LoadXMLData(const XMLData: string): IXMLDocument; overload;
function NewXMLDocument(Version: DOMString = '1.0'): IXMLDocument;
可以看出這些函數(shù)全部返回的是IXMLDocument接口,得到了IXMLDocument接口在進行文檔的操作;這些函數(shù)都是通過創(chuàng)建TXMLDocument對象來實現(xiàn)對XML文檔的讀取的;其中NewXMLDocument僅僅創(chuàng)建一個IXMLDocument接口。
可以這樣利用NewXMLDocument來讀取XML文檔:
XMLDoc := NewXMLDocument;
XMLDoc.LoadFromFile(FileName);
·保存XML文檔
可以通過下面的方式來保存XML文檔:
XMLDoc := NewXMLDocument;
iRoot := IXMLDoc.CreateNode(‘TestXMLDocument’);
XMLDoc.DocumentElement := iRoot;
…
XMLDoc.SaveToFile(FileName);
可以看出通過接口來操作XML文檔是非常方便的;
選用不同類型的XML解析方式