将XML节点绑定到树视图节点 [英] Binding an XML node to a Tree view node

查看:137
本文介绍了将XML节点绑定到树视图节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用TTreeView浏览XML。要将树视图节点与属性的XML节点相关联,我使用以下语法:

I want to browse through XML using a TTreeView. To associate the treeview nodes with the XML nodes with attributes I used the following syntax:

var tv: TTreeView; tn1, tn2: TTreeNode; xn: IXMLNode;

if xn.AttributeNodes.Count > 0 then
  tn2 := tv.Items.AddChildObject( tn1, xn.NodeName, @xn )
else
  tn2 := tv.Items.AddChild( tn1, xn.NodeName );

..以后在程序中:

var  tv: TTreeView; pxn: ^IXMLNode; i: integer;

pxn := tv.Selected.Data;
for i := 0 to iXML.AttributeNodes.Count-1 do
  ShowMessage ( pxn^.AttributeNodes[i].LocalName + ' = ' +
                pxn^.AttributeNodes[i].Text );

这会导致异常。据我所知,这与事实有关我指向一个接口而不是一个对象。

which results in an exception.. As far as I can figure out this has to do with the fact that I'm point to an interface instead of an object.

可以引用XML的实际对象而不是接口吗?
如果新的XML节点插入树中或从树中删除,这个参考将会发生什么?

Is it possible to reference the actual object of the XML instead of the interface? What will happen with this reference if new XML nodes are inserted in or deleted from the tree?

还是有另一个直接解决方案?

Or is there another straight forward solution?

所有帮助赞赏

推荐答案

不要使用@和^已经参考

don't use @ and ^ operators, interfaces are already references

第一个代码:

var tv: TTreeView; tn1, tn2: TTreeNode; xn: IXMLNode;

if xn.AttributeNodes.Count > 0 then
  tn2 := tv.Items.AddChildObject( tn1, xn.NodeName, Pointer(xn) )
else
  tn2 := tv.Items.AddChild( tn1, xn.NodeName );

第二个代码(不要忘记检查数据是否分配)

second code (don't forget to check if data is assigned)

var  tv: TTreeView; pxn: IXMLNode; i: integer;

if Assigned(tv.Selected) and Assigned(tv.Selected.Data) then begin
  pxn := IXMLNode(tv.Selected.Data);
  for i := 0 to iXML.AttributeNodes.Count-1 do
    ShowMessage ( pxn.AttributeNodes[i].LocalName + ' = ' +
                  pxn.AttributeNodes[i].Text );
end;

只需在网络中搜索有关接口,类及其区别的更多信息。好消息: http://blog.synopse.info/post/ 2012/02/29 / Delphi和界面

Just search in the net for more information about interfaces, classes and the differences between them. Good information: http://blog.synopse.info/post/2012/02/29/Delphi-and-interfaces

这篇关于将XML节点绑定到树视图节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆