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

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

问题描述

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



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

解决方案

您可以先看看Delphi的TXMLDocument组件。这将为您提供使用XML / DOM的基础知识。它很简单,可以通过将其添加到您的窗体中来添加。它具有LoadFromFile和SaveToFile方法,并且很容易导航。



然而,在某些时候,您将耗尽TXMLDocument的功能,特别是如果您想使用XPath等操作。 p>

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

  XML:= CreateOleObject('MSXML2.DOMDocument.3.0')为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

如果语言<>''然后
xml.SetProperty('SelectionLanguage','XPath');

如果编码<>''然后开始
NewPI:= xml.createProcessingInstruction('xml','version =1.0encoding ='+ encoding +'
xml.appendChild(NewPI);
结束

如果xsl <'然后开始
NewPI:= xml.createProcessingInstruction('xml-stylesheet','type =text / xslhref ='+ xsl +' ');
xml.appendChild(NewPI)
end;

xml.async:= false;
xml.documentElement:= xml.createElement(RootName);
结果:= xml.documentElement;
结束

从那里拿走。


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.

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

解决方案

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.

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

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');

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;

Take it from there.

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

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