Delphi 中的 XML 库和教程 [英] Libraries and tutorials for XML in Delphi

查看:41
本文介绍了Delphi 中的 XML 库和教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划为应用程序添加 XML 支持,但我不熟悉 Delphi 中的 XML 编程.基本上我需要基于 XML 节点创建对象并基于对象生成 XML 文件.

I'm planning to add XML support to application, but I'm not familiar with XML programming in Delphi. Basically I need to create objects based on XML nodes and generate XML file based on objects.

我应该使用哪个 XML 组件库?有没有关于 Delphi 的 XML 的好教程?

Which XML component library I should use? Are there any good tutorials for XML with Delphi?

推荐答案

您可以从查看 Delphi 的 TXMLDocument 组件开始.这将为您提供使用 XML/DOM 的基础知识.它很简单,可以通过将其拖放到您的表单上来添加.它具有 LoadFromFile 和 SaveToFile 方法,并且易于导航.

You can start by looking at Delphi's TXMLDocument component. This will provide you with the basics of working with XML/DOM. It's simple and can be added by dropping it onto your Form. It has LoadFromFile and SaveToFile methods and is easily navigated.

但是,在某些时候您将耗尽 TXMLDocument 的功能,尤其是当您想使用 XPath 之类的东西时.

However, at some point you will exhaust TXMLDocument's features, especially if you want to work with things like XPath.

我建议您查看 IXMLDOMDocument2,它是 MSXML2_TLB 的一部分,例如

I suggest you look at IXMLDOMDocument2 which is part of MSXML2_TLB, e.g.

  XML := CreateOleObject('MSXML2.DOMDocument.3.0') as IXMLDOMDocument2;
  XML.async := false;
  XML.SetProperty('SelectionLanguage','XPath');

您需要添加 msxmldom、xmldom、XMLIntf、XMLDoc &MSXML2_TLB 到您的用途部分.

You will need to add msxmldom, xmldom, XMLIntf, XMLDoc & MSXML2_TLB to your uses section.

那里有一些组件库,但我建议您编写自己的帮助程序类或函数.这是我们编写和使用的示例:

There are a few component libraries out there but I would suggest writing your own helper class or functions. Here's an example of one we wrote and use:

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;

从那里拿走.

这篇关于Delphi 中的 XML 库和教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆