Delphi 中遍历 XML 文档的完整示例
2007-10-20 08:13
204 查看
XML 文档的节点是标准的树形结构,可以通过递归或者回溯算法来遍历所有的节点。
本例使用递归算法为例,制作了2个遍历 XML 文档的例子。
第一个例子,给出一个 XML 节点,遍历所有子节点-不包括起始节点。
第二个例子,给出一个 XML 节点,遍历所给的节点及其下属的所有子节点。
以上2个示例均列举出了 XML 文档的标签属性和文本值,是比较完整的示例代码。
<?xml version="1.0" encoding="gb2312"?>
<root website="http://www.why100000.com">
<channel id="1" tagname="channel">
<topic id="1-1" tagname="topic">Windows频道</topic>
<Url id="1-2" tagname="Url">www.why100000.com/_windows</Url>
</channel>
<channel id="2" tagname="channel">
<topic id="2-1" tagname="topic">代码实验室</topic>
<subWeb id="2-2" tagname="subWeb">http://lab.why100000.com</subWeb>
</channel>
<BBS id="03" tagname="BBS">
<topic id="03-1" tagname="topic">电脑学习社区</topic>
<subWeb id="03-2" tagname="subWeb">http://bbs.why100000.com</subWeb>
<subBBS id="03-3">
<subBBStopic id="03-3-1">菜鸟学院</subBBStopic>
<subBBStopic id="03-3-2">问吧</subBBStopic>
<subBBStopic id="03-3-3">学吧</subBBStopic>
<subBBStopic id="03-3-4">回吧</subBBStopic>
<subBBStopic id="03-3-5"></subBBStopic>
<subBBStopic></subBBStopic>
</subBBS>
</BBS>
<AnatherTag note="testTag-1"/>
<AnatherTag/>
</root>
{----------------------------------------------------------------------}
function TForm1.GetXmlTree1(nNode: IXMLNode): string;
var
i, j: integer;
begin
for i:=0 to nNode.ChildNodes.Count-1 do
begin
s := s + '<' + nNode.ChildNodes.Nodes[i].NodeName;
for j:=0 to nNode.ChildNodes[i].AttributeNodes.Count-1 do
begin
s := s + ' '+ nNode.ChildNodes[i].AttributeNodes[j].NodeName;
s := s + '="'+ nNode.ChildNodes[i].AttributeNodes[j].NodeValue +'"';
end;
s := s + '>';
if nNode.ChildNodes.Nodes[i].IsTextElement then
s := s + nNode.ChildNodes.Nodes[i].Text;
if nNode.HasChildNodes and not nNode.ChildNodes.Nodes[i].IsTextElement then
begin
s := s + #13 + #10;
GetXmlTree1(nNode.ChildNodes.Nodes[i]);
end;
s := s + '</'+ nNode.ChildNodes.Nodes[i].NodeName +'>' + #13 + #10;
end;
result := s;
end;
{----------------------------------------------------------------------}
function TForm1.GetXmlTree2(nNode: IXMLNode): string;
var
i, j: integer;
begin
s := s + '<' + nNode.NodeName;
for j:=0 to nNode.AttributeNodes.Count-1 do
begin
s := s + ' '+ nNode.AttributeNodes[j].NodeName;
s := s + '="'+ nNode.AttributeNodes[j].NodeValue +'"';
end;
s := s + '>';
if nNode.IsTextElement then
s := s + nNode.Text
else
begin
s := s + #13 + #10;
if nNode.HasChildNodes then
for i:=0 to nNode.ChildNodes.Count-1 do
begin
GetXmlTree2(nNode.ChildNodes.Nodes[i]);
end;
end;
s := s + '</' + nNode.NodeName + '>'+ #13 + #10;
result := s;
end;
{----------------------------------------------------------------------}
调用:
procedure TForm1.Button4Click(Sender: TObject);
var
oXml: TXMLDocument;
begin
oXml := TXMLDocument.Create(self);
oXml.FileName := '_Treeview.xml';
oXml.Active:=true;
s :='';
s := GetXmlTree1(oXml.ChildNodes.FindNode('root'));
Memo1.Lines.Add(s);
oXml.Free;
end;
procedure TForm1.Button5Click(Sender: TObject);
var
oXml: TXMLDocument;
begin
oXml := TXMLDocument.Create(self);
oXml.FileName := '_Treeview.xml';
oXml.Active:=true;
s :='';
s := GetXmlTree2(oXml.ChildNodes.FindNode('root'));
Memo1.Lines.Add(s);
oXml.Free;
end;
“十万个为什么”电脑学习网原创文档
转载请注明出处:http://www.why100000.com
本例使用递归算法为例,制作了2个遍历 XML 文档的例子。
第一个例子,给出一个 XML 节点,遍历所有子节点-不包括起始节点。
第二个例子,给出一个 XML 节点,遍历所给的节点及其下属的所有子节点。
以上2个示例均列举出了 XML 文档的标签属性和文本值,是比较完整的示例代码。
<?xml version="1.0" encoding="gb2312"?>
<root website="http://www.why100000.com">
<channel id="1" tagname="channel">
<topic id="1-1" tagname="topic">Windows频道</topic>
<Url id="1-2" tagname="Url">www.why100000.com/_windows</Url>
</channel>
<channel id="2" tagname="channel">
<topic id="2-1" tagname="topic">代码实验室</topic>
<subWeb id="2-2" tagname="subWeb">http://lab.why100000.com</subWeb>
</channel>
<BBS id="03" tagname="BBS">
<topic id="03-1" tagname="topic">电脑学习社区</topic>
<subWeb id="03-2" tagname="subWeb">http://bbs.why100000.com</subWeb>
<subBBS id="03-3">
<subBBStopic id="03-3-1">菜鸟学院</subBBStopic>
<subBBStopic id="03-3-2">问吧</subBBStopic>
<subBBStopic id="03-3-3">学吧</subBBStopic>
<subBBStopic id="03-3-4">回吧</subBBStopic>
<subBBStopic id="03-3-5"></subBBStopic>
<subBBStopic></subBBStopic>
</subBBS>
</BBS>
<AnatherTag note="testTag-1"/>
<AnatherTag/>
</root>
{----------------------------------------------------------------------}
function TForm1.GetXmlTree1(nNode: IXMLNode): string;
var
i, j: integer;
begin
for i:=0 to nNode.ChildNodes.Count-1 do
begin
s := s + '<' + nNode.ChildNodes.Nodes[i].NodeName;
for j:=0 to nNode.ChildNodes[i].AttributeNodes.Count-1 do
begin
s := s + ' '+ nNode.ChildNodes[i].AttributeNodes[j].NodeName;
s := s + '="'+ nNode.ChildNodes[i].AttributeNodes[j].NodeValue +'"';
end;
s := s + '>';
if nNode.ChildNodes.Nodes[i].IsTextElement then
s := s + nNode.ChildNodes.Nodes[i].Text;
if nNode.HasChildNodes and not nNode.ChildNodes.Nodes[i].IsTextElement then
begin
s := s + #13 + #10;
GetXmlTree1(nNode.ChildNodes.Nodes[i]);
end;
s := s + '</'+ nNode.ChildNodes.Nodes[i].NodeName +'>' + #13 + #10;
end;
result := s;
end;
{----------------------------------------------------------------------}
function TForm1.GetXmlTree2(nNode: IXMLNode): string;
var
i, j: integer;
begin
s := s + '<' + nNode.NodeName;
for j:=0 to nNode.AttributeNodes.Count-1 do
begin
s := s + ' '+ nNode.AttributeNodes[j].NodeName;
s := s + '="'+ nNode.AttributeNodes[j].NodeValue +'"';
end;
s := s + '>';
if nNode.IsTextElement then
s := s + nNode.Text
else
begin
s := s + #13 + #10;
if nNode.HasChildNodes then
for i:=0 to nNode.ChildNodes.Count-1 do
begin
GetXmlTree2(nNode.ChildNodes.Nodes[i]);
end;
end;
s := s + '</' + nNode.NodeName + '>'+ #13 + #10;
result := s;
end;
{----------------------------------------------------------------------}
调用:
procedure TForm1.Button4Click(Sender: TObject);
var
oXml: TXMLDocument;
begin
oXml := TXMLDocument.Create(self);
oXml.FileName := '_Treeview.xml';
oXml.Active:=true;
s :='';
s := GetXmlTree1(oXml.ChildNodes.FindNode('root'));
Memo1.Lines.Add(s);
oXml.Free;
end;
procedure TForm1.Button5Click(Sender: TObject);
var
oXml: TXMLDocument;
begin
oXml := TXMLDocument.Create(self);
oXml.FileName := '_Treeview.xml';
oXml.Active:=true;
s :='';
s := GetXmlTree2(oXml.ChildNodes.FindNode('root'));
Memo1.Lines.Add(s);
oXml.Free;
end;
“十万个为什么”电脑学习网原创文档
转载请注明出处:http://www.why100000.com
相关文章推荐
- Delphi 中遍历 XML 文档的完整示例
- Delphi 中遍历 XML 文档的完整示例
- Delphi 中遍历 XML 文档的完整示例
- 如何使用WebBrowser控件打印格式化的XML文档,并以编程方式任意设置打印方向(C#完整示例)
- 使用Delphi解析XML 文档
- 循环递归遍历XML文档或按某要求遍历XML文档
- 大容量导入和导出 XML 文档的示例
- XML解析之sax解析案例(一)读取contact.xml文件,完整输出文档内容
- microsoft.xmldom(一) xml文档遍历js
- XSLT语法 在.net中使用XSLT转换xml文档示例
- 《GOF设计模式》—享元(FLYWEIGHT)—Delphi源码示例:文档编辑器
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
- QT 遍历多层次的XML文档
- Java_Ant_Web Project完整build.xml文件模板以及Strust2简单示例;
- 设置搜索Flex 4.6 XML搜索、匹配示例,完整代码
- Delphi 中用 Xml 配置文档生成 Treeview
- 在delphi中使用xml文档有两种方法
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)