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. 如何在Axure中使用FontAwesome字体图标

    Font Awesome为您提供可缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小.颜色.阴影或者其它任何支持的效果. FontAwesome应用在web网页开发中非常方便, ...

  2. 再谈CentOS 7程序自启动

    上次发现了/etc/init.d下已经没有启动脚本了,然后对于启动乱序自己在rc.local中重排. 其实想一想这些应用的自启动终归还是需要通过脚本来执行的. 一.脚本在哪里? /usr/lib/sy ...

  3. PHP科学计数法转换成数字

    /** * 科学计数法转换成数字 * @param $num * @param int $double * @return int */ function sctonum($num, $double ...

  4. LINQ基本语句

    一.什么是LINQ 1.定义:LINQ是Language Integrate Query的缩写,它在对象和数据之间建立一种对应关系,可以使用访问内存对象的方式查询数据集合. 2.特点:由于LINQ中查 ...

  5. 如何写django中form的测试用例

    可简可繁, 可插库,可字符, 要测试valid,也要测试invalid, 可用csrf,也可用context. 放一个全面的, 实践中,找一个最优的组合就好. class NewTopicTests( ...

  6. [转] HTML5之FileReader的使用

    HTML5定义了FileReader作为文件API的重要成员用于读取文件,根据W3C的定义,FileReader接口提供了读取文件的方法和包含读取结果的事件模型. FileReader的使用方式非常简 ...

  7. MVC中使用Web API和EntityFramework

    在ASP.NET MVC中使用Web API和EntityFramework构建应用程序   最近做了一个项目技术预研:在ASP.NET MVC框架中使用Web API和EntityFramework ...

  8. 008 使用POJO对象绑定请求参数

    1.介绍 2.Person.java package com.spring.bean; public class Person { private String username; private S ...

  9. 如何使用DSP的cache(转)

    C6747在执行一块算法的执行时间在114ms左右,需求要20ms以下.6000属于分层存储器体系架构,内部RAM跟CPU不同频运行,只有cache使能才跟CPU同频.可能是cache没打开.下面转载 ...

  10. luogu [TJOI2007]线段

    题目链接 luogu [TJOI2007]线段 题解 dp[i][0/1]第i行在左/右端点的最短路 瞎转移 代码 #include<bits/stdc++.h> using namesp ...