加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi中的XML和库教程

发布时间:2020-12-15 10:17:38 所属栏目:大数据 来源:网络整理
导读:我计划为应用程序添加XML支持,但我不熟悉Delphi中的XML编程。 基本上我需要创建基于XML节点的对象,并基于对象生成XML文件。 我应该使用哪个XML组件库?有没有什么好的教程与XML与Delphi? 解决方法 您可以先看看Delphi的TXMLDocument组件。这将为您提供使
我计划为应用程序添加XML支持,但我不熟悉Delphi中的XML编程。
基本上我需要创建基于XML节点的对象,并基于对象生成XML文件。

我应该使用哪个XML组件库?有没有什么好的教程与XML与Delphi?

解决方法

您可以先看看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到您的使用部分。

有几个组件库,但我建议您编写自己的助手类或函数。这是我们写和使用的一个例子:

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;

从那里拿走

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读