我计划在应用程序中添加XML支持,但我不熟悉Delphi中的XML编程。基本上,我需要基于XML节点创建对象,并基于对象生成XML文件。
我应该使用哪个XML组件库?对于使用Delphi的XML,有什么好的教程吗?
发布于 2008-11-04 21:35:56
您可以从查看Delphi的TXMLDocument组件开始。这将为您提供使用XML/DOM的基础知识。它很简单,可以通过将其拖放到您的表单中来添加。它有LoadFromFile和SaveToFile方法,并且很容易导航。
但是,在某些情况下,您会耗尽TXMLDocument的功能,特别是当您想要使用XPath之类的东西时。
我建议你看看IXMLDOMDocument2,它是MSXML2_TLB的一部分。
XML := CreateOleObject('MSXML2.DOMDocument.3.0') as IXMLDOMDocument2;
XML.async := false;
XML.SetProperty('SelectionLanguage','XPath');您需要将msxmldom、xmldom、XMLIntf、XMLDoc和MSXML2_TLB添加到您的使用部分。
有一些组件库,但我建议您编写自己的helper类或函数。下面是我们编写和使用的一个示例:
function XMLCreateRoot(var xml: IXMLDOMDocument2; RootName: string; xsl: string = ''; encoding: string = 'ISO-8859-1'; language: string = 'XPath'): IXMLDOMNode;
var
NewPI: IXMLDOMProcessingInstruction;
begin
if language<>'' then
xml.SetProperty('SelectionLanguage','XPath');
if encoding<>'' then begin
NewPI:=xml.createProcessingInstruction('xml', 'version="1.0" encoding="'+encoding+'"');
xml.appendChild(NewPI);
end;
if xsl<>'' then begin
NewPI:=xml.createProcessingInstruction('xml-stylesheet','type="text/xsl" href="'+xsl+'"');
xml.appendChild(NewPI)
end;
xml.async := false;
xml.documentElement:=xml.createElement(RootName);
Result:=xml.documentElement;
end;从那里开始吧。
发布于 2008-11-05 08:36:53
我使用的是simdesign中的nativeXML。它消除了使用XML的所有痛苦,您可以在几分钟内启动并运行它。
发布于 2008-11-04 21:33:19
以下是几个教程:
此外,您可能想要查看XMLIntf unit (尽管这个链接的Delphi Wikia页面的内容非常少)。
https://stackoverflow.com/questions/263419
复制相似问题