Description

This example illustrates the basic operations on an XML document.

Code

procedure CreateDocument;
const
CFilename = 'file.xml';
var
LDocument: IXMLDocument;
LNodeElement, NodeCData, NodeText: IXMLNode;
begin
LDocument := TXMLDocument.Create(nil);
LDocument.Active := True; { Define document content. }
LDocument.DocumentElement := LDocument.CreateNode('ThisIsTheDocumentElement', ntElement, '');
LDocument.DocumentElement.Attributes['attrName'] := 'attrValue';
LNodeElement := LDocument.DocumentElement.AddChild('ThisElementHasText', -1);
LNodeElement.Text := 'Inner text.';
NodeCData := LDocument.CreateNode('any characters here', ntCData, '');
LDocument.DocumentElement.ChildNodes.Add(NodeCData);
NodeText := LDocument.CreateNode('This is a text node.', ntText, '');
LDocument.DocumentElement.ChildNodes.Add(NodeText); LDocument.SaveToFile(CFilename);
end; procedure RetrieveDocument;
const
CFilename = 'file.xml';
CAttrName = 'attrName';
HTAB = #9;
var
LDocument: IXMLDocument;
LNodeElement, LNode: IXMLNode;
LAttrValue: string;
I: Integer;
begin
LDocument := TXMLDocument.Create(nil);
LDocument.LoadFromFile(CFilename); { Find a specific node }
LNodeElement := LDocument.ChildNodes.FindNode('ThisIsTheDocumentElement'); if (LNodeElement <> nil) then
begin
{ Get a specific attribute }
Writeln('Getting attribute...');
if (LNodeElement.HasAttribute(CAttrName)) then
begin
LAttrValue := LNodeElement.Attributes[CAttrName];
Writeln('Attribute value: ' + LAttrValue);
end; { Traverse child nodes }
Writeln(sLineBreak, 'Traversing child nodes...' + sLineBreak);
for I := 0 to LNodeElement.ChildNodes.Count - 1 do
begin
LNode := LNodeElement.ChildNodes.Get(I);
{ Display node name }
Writeln(sLineBreak + 'Node name: ' + LNode.NodeName);
{ Check if the node type is Text. }
if LNode.NodeType = ntText then
begin
Writeln(HTAB + 'This is a node of type Text. The text is: ' + LNode.Text);
end;
{ Check if the node is text element. }
if LNode.IsTextElement then
begin
Writeln(HTAB + 'This is a text element. The text is: ' + LNode.Text);
end;
end;
end;
end;

TXMLDocument use case (Delphi)的更多相关文章

  1. Delphi中使用TXMLDocument控件应注意的问题 转

    Delphi中使用TXMLDocument控件应注意的问题 delphiconstructorxmlclass今天写了一个类,其中用到了TXMLDocument控件.这个控件我是要动态生成的. 但是却 ...

  2. Delphi中TxmlDocument控件的用法 转

    Delphi中对XML文件的解析做的很好,比直接使用MS的MSXML2_TLB中的接口要方便很多,现称述于下面. 在讲之前先给出一个XML实例,在讲某些部分是要结合实例比较容易理解. 1<?xm ...

  3. Delphi中使用TXMLDocument控件应注意的问题

    今天写了一个类,其中用到了TXMLDocument控件.这个控件我是要动态生成的. 但是却遇到了非常奇怪的问题,下面分享一下 procedure TMainForm.Button1Click(Send ...

  4. delphi中Case语法的使用方法

    Case 语句If...Then…Else 语句适合选项较少的情况,如果有很多选项的话利用If 语句就比较麻烦,在这种情况下,Case 语句就容易多了.Case 语句的语法如下: case <表 ...

  5. Delphi中动态调用TXMLDocument的经历

    var  vXMLDocument: TXMLDocument;begin  vXMLDocument := TXMLDocument.Create('c:/temp/temp.xml');  Cap ...

  6. Delphi 复习代码

    1.取得可文件路径 Path := ExtractFilePath(Application.ExeName); //取得可执行文件路径 TXMLDocument.Create(ExtractFileP ...

  7. Delphi Xml

    用递归方法,使用 xml 文档生成 Treeview 树形视图.由于是动态生成,所以可以通过修改 xml 的逻辑来定制 Treeview 的结构,从而实现了 xml 对 Treeview 的动态配置, ...

  8. Delphi容器类之---Tlist,TStringlist,THashedStringlist的效率比较

    转载自:http://www.ylzx8.cn/windows/delphi/73200.html 本人在做一个测试,服务器是IOCP的,我假定最大链接数是50000个. 测试背景:如果每个链接之间的 ...

  9. Delphi容器类之---TList、TStringList、TObjectList,以及一个例程的代码分析

    转载自:http://blog.csdn.net/jqandjq/article/details/5429137 看了这里标题,大家可能以为我会谈TListBox控件,那就错了.我要谈的是Delphi ...

随机推荐

  1. 【Hadoop】搭建完全分布式的hadoop【转】

    转自:http://www.cnblogs.com/laov/p/3421479.html 下面博文已更新,请移步 ↑ 用于测试,我用4台虚拟机搭建成了hadoop结构 我用了两个台式机.一个xp系统 ...

  2. dblinks

    一.Oracle数据库链Database links的作用 当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本 ...

  3. IDEA配置文件的配置文件配置

    IDEA配置文件的配置文件配置: 路径 /Applications/IntelliJ IDEA 3.app/Contents/bin/idea.vmoptions (/IntelliJ IDEA 3. ...

  4. 八、vue使用element-ui组件

    element-ui组件 1.引入element import Vue from 'vue'; import ElementUI from 'element-ui'; import 'element- ...

  5. NOIp 2018 普及组

    T1标题统计 传送门 题目描述 凯凯刚写了一篇美妙的作文,请问这篇作文的标题中有多少个字符? 注意:标题中可能包含大.小写英文字母.数字字符.空格和换行符.统计标题字 符数时,空格和换行符不计算在内. ...

  6. 001_Eclipse编写第一个Java程序

    1 下载并安装jdk 2 下载较新版本的eclipse,eclipse都是非安装版的,解压缩即可. 3 双击eclipse.exe,打开elipse软件 4 FileàNewàProject 5 选择 ...

  7. Archlinux安装笔记

    由于自己的电脑上暂时还没有足够的硬盘空间,所以先暂用虚拟机来装一下Archlinux,在安装好VMware Workstation之后,从Archlinux官网的下载页面下载到了最新的镜像文件.之后新 ...

  8. 第二届CCF软件能力认证

    1. 相邻数对 问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. 输入格式 输入的第一行包含一个整数n,表示给定整数的个数. 第二行包含所给定的n个整数. 输出格式 输出一个 ...

  9. django用户系统的测试,蛮不错的。

    https://blog.csdn.net/orangleliu/article/details/51944758 这个很实用 https://www.cnblogs.com/yanhuidj/p/1 ...

  10. ld: warning: directory not found for option '-F/Users/Jason/Project/xxx'

    解决方法: 选择项目名称----->Targets----->Build Settings----->Search Paths----->Library Search Path ...