很简单啊!
delphi的例子:
...
var
XMLDoc: IXMLDomDocument;
begin
XMLDoc:=CoDOMDocument.Create ;
XMLDoc.load('./test_doc/lane-import.xml');
if xmltotree(treeview1,XMLDoc,True)=false then
application.MessageBox('转换失败'+chr(VK_Tab),'信息提示',MB_ok) ;
end;
...
...
function XmlToTree(sourcetree :TTreeview;XMLDocument:IXMLDomDocument;atrflag:Boolean):Boolean;
var
root: IXMLDomNode;
F_treenode:TTreeNode;
begin
//将xml转换为tree
if (sourcetree=nil) or (XMLDocument=nil) then
//判断树和xml文档是否为空
begin
result:=false;
exit;
end;
root:=XMLDocument.DocumentElement;
if root=nil then
//判断xml文档首节点是否为空
begin
result:=false;
exit;
end;
sourcetree.Items.Add(nil,root.NodeName);
F_treenode:=sourcetree.Items[0];
if atrflag then
addxmlAttributeNodes(sourcetree,F_treenode,root);
xmltotree(sourcetree,F_treenode,root,atrflag);
result:=True;
end;
function XmlToTree(sourcetree :TTreeview;F_treenode:TTreeNode;root:IXMLDomNode;atrflag:Boolean):Boolean;
var
Stock: IXMLDomNode;
i:Integer;
node_name:string;
// 节点的名称
c_treenode:TTreeNode;
begin
//增加节点的子节点以及其属性节点和子节点
if (sourcetree=nil) or (root=nil) then
begin
result:=false;
exit;
end;
for i:=0 to root.ChildNodes.length-1do
begin
node_name:=root.ChildNodes.NodeName;
if pos('#',node_name)>0 then
break
else
begin
Stock:=root.ChildNodes;
c_treenode:=sourcetree.Items.AddChild(F_treenode,node_name);
if atrflag then
addxmlAttributeNodes(sourcetree,c_treenode,stock);
xmltotree(sourcetree,c_treenode,stock,atrflag);
end;
end;
result:=True;
end;
function AddXmlAttributeNodes(sourcetree :TTreeview;F_treenode:TTreeNode;father_xml: IXMLDomNode):Boolean;
var
i: Integer;
node_name:string;
// 节点的名称
begin
//增加节点的属性节点
if (sourcetree=nil) or (father_xml=nil) then
begin
result:=false;
exit;
end;
if father_xml.attributes.length >0 then
begin
for i:=0 to father_xml.attributes.length-1do
begin
node_name:='@' + father_xml.Attributes.NodeName;
sourcetree.Items.AddChild(F_treenode,node_name);
end;
end;
result:=True;
end;
...
我随便做着玩的例子!见笑了!