黄聪:Delphi 中的 XMLDocument 类详解(14) - 遍历 XML 文件
2010-08-27 11:31
531 查看
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls;
type
TForm1 = class(TForm)
XMLDocument1: TXMLDocument;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//读取 xml 的函数
{
功能1: 传入一个节点参数, 返回节点及其包含的所有内容;
功能2: 排除了空节点.
}
function ReadXml(node: IXMLNode): string;
var
nodeList,attrList: IXMLNodeList;
str,strName,strValue: string;
i: Integer;
begin
Result := '';
if not node.HasChildNodes then Exit;
attrList := node.AttributeNodes; {根节点的属性列表}
nodeList := node.ChildNodes; {根节点下的子节点列表}
str := '<' + node.NodeName;
{先读取属性}
for i := 0 to attrList.Count - 1 do
begin
strName := attrList[i].NodeName;
strValue := attrList[i].NodeValue;
str := str + ' ' + strName + '=' + AnsiQuotedStr(strValue, '"');
end;
str := str + '>' + sLineBreak; {sLineBreak 是常量, 相当于 #13#10}
{读取子节点}
for i := 0 to nodeList.Count - 1 do
begin
strName := nodeList[i].NodeName;
if nodeList[i].IsTextElement then
begin
strValue := nodeList[i].NodeValue;
str := str + '<' + strName + '>' + strValue + '</' + strName + '>' + sLineBreak;
end else if nodeList[i].HasChildNodes then
begin
str := str + ReadXml(nodeList[i]); {这是最关键的递归调用}
str := str + '</' + strName + '>' + sLineBreak; {封口}
end;
end;
str := str + '</' + node.NodeName + '>'; {封口}
Result := str;
end;
//调用测试(1):
procedure TForm1.Button1Click(Sender: TObject);
var
str,s1,s2: string;
begin
XMLDocument1.LoadFromFile('c:\temp\test.xml');
{必须用万一提供的 xml 测试文件, 才能有相同的返回值}
{读取文件头}
s1 := AnsiQuotedStr(XMLDocument1.Version, '"'); {读出版本, 并添加双引号}
s2 := AnsiQuotedStr(XMLDocument1.Encoding, '"'); {读出字符集, 并添加双引号}
str := Format('<?xml version=%s encoding=%s?>',[s1,s2]); {这就是文件头了}
str := str + sLineBreak + ReadXml(XMLDocument1.DocumentElement);
ShowMessage(str); {返回 xml 包含问头在内的所有内容}
end;
//调用测试(2)
procedure TForm1.Button2Click(Sender: TObject);
var
str: string;
node: IXMLNode;
begin
XMLDocument1.LoadFromFile('c:\temp\test.xml');
node := XMLDocument1.DocumentElement.ChildNodes[0];
str := ReadXml(node);
ShowMessage(str); {返回返回根节点下第一个子节点的所有内容}
end;
end.
出处:http://www.cnblogs.com/del/archive/2008/01/03/1025133.html
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls;
type
TForm1 = class(TForm)
XMLDocument1: TXMLDocument;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//读取 xml 的函数
{
功能1: 传入一个节点参数, 返回节点及其包含的所有内容;
功能2: 排除了空节点.
}
function ReadXml(node: IXMLNode): string;
var
nodeList,attrList: IXMLNodeList;
str,strName,strValue: string;
i: Integer;
begin
Result := '';
if not node.HasChildNodes then Exit;
attrList := node.AttributeNodes; {根节点的属性列表}
nodeList := node.ChildNodes; {根节点下的子节点列表}
str := '<' + node.NodeName;
{先读取属性}
for i := 0 to attrList.Count - 1 do
begin
strName := attrList[i].NodeName;
strValue := attrList[i].NodeValue;
str := str + ' ' + strName + '=' + AnsiQuotedStr(strValue, '"');
end;
str := str + '>' + sLineBreak; {sLineBreak 是常量, 相当于 #13#10}
{读取子节点}
for i := 0 to nodeList.Count - 1 do
begin
strName := nodeList[i].NodeName;
if nodeList[i].IsTextElement then
begin
strValue := nodeList[i].NodeValue;
str := str + '<' + strName + '>' + strValue + '</' + strName + '>' + sLineBreak;
end else if nodeList[i].HasChildNodes then
begin
str := str + ReadXml(nodeList[i]); {这是最关键的递归调用}
str := str + '</' + strName + '>' + sLineBreak; {封口}
end;
end;
str := str + '</' + node.NodeName + '>'; {封口}
Result := str;
end;
//调用测试(1):
procedure TForm1.Button1Click(Sender: TObject);
var
str,s1,s2: string;
begin
XMLDocument1.LoadFromFile('c:\temp\test.xml');
{必须用万一提供的 xml 测试文件, 才能有相同的返回值}
{读取文件头}
s1 := AnsiQuotedStr(XMLDocument1.Version, '"'); {读出版本, 并添加双引号}
s2 := AnsiQuotedStr(XMLDocument1.Encoding, '"'); {读出字符集, 并添加双引号}
str := Format('<?xml version=%s encoding=%s?>',[s1,s2]); {这就是文件头了}
str := str + sLineBreak + ReadXml(XMLDocument1.DocumentElement);
ShowMessage(str); {返回 xml 包含问头在内的所有内容}
end;
//调用测试(2)
procedure TForm1.Button2Click(Sender: TObject);
var
str: string;
node: IXMLNode;
begin
XMLDocument1.LoadFromFile('c:\temp\test.xml');
node := XMLDocument1.DocumentElement.ChildNodes[0];
str := ReadXml(node);
ShowMessage(str); {返回返回根节点下第一个子节点的所有内容}
end;
end.
出处:http://www.cnblogs.com/del/archive/2008/01/03/1025133.html
相关文章推荐
- Delphi 中的 XMLDocument 类详解(14) - 遍历 XML 文件
- 黄聪:Delphi 中的 XMLDocument 类详解(3) - 读取 xml 文件
- 黄聪:Delphi 中的 XMLDocument 类详解(16) - 节点列表中的第一个与最后一个节点
- 黄聪:Delphi 中的 XMLDocument 类详解(17) - 上一个节点、下一个节点、父节点
- 黄聪:Delphi 中的 XMLDocument 类详解(18) - 更好地显示 xml 的测试结果 - FormatXMLData 及其他
- 黄聪:Delphi 中的 XMLDocument 类详解(6) - 访问节点属性
- 黄聪:Delphi 中的 XMLDocument 类详解(19) - NodeValue 与 NodeName 的读写区别
- Delphi 中的 XMLDocument 类详解(3) - 读取 xml 文件
- 黄聪:Delphi 中的 XMLDocument 类详解(7) - 获取节点名称与节点的属性名称
- 黄聪:Delphi 中的 XMLDocument 类详解(20) - 动态建立 XMLDocument 对象
- Delphi 中的 XMLDocument 类详解(3) - 读取 xml 文件
- 黄聪:Delphi 中的 XMLDocument 类详解(1) - 等待研究的内容
- 黄聪:Delphi 中的 XMLDocument 类详解(2) - 记要
- 黄聪:Delphi 中的 XMLDocument 类详解(8) - 添加与删除节点
- 黄聪:Delphi 中的 XMLDocument 类详解(9) - 关于 HasChildNodes 与 IsTextElement
- Delphi 中的 XMLDocument 类详解(3) - 读取 xml 文件
- 黄聪:Delphi 中的 XMLDocument 类详解(10) - 判断节点类型: 支节点、叶节点、文本节点、空节点
- 黄聪:Delphi 中的 XMLDocument 类详解(11) - 一个多余的属性: Nodes
- 黄聪:Delphi 中的 XMLDocument 类详解(4) - 获取根目录下的元素数
- 黄聪:Delphi 中的 XMLDocument 类详解(12) - 读取或设置 xml 的编码与版本号