Binding XML node with tree view node

I want to view XML using TTreeView. To link tree nodes to 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 );

      

.. and later in the program:

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 understand it has to do with the fact that I am pointing to an interface instead of an object.

Is it possible to reference an actual XML object instead of an interface? What happens to this link if new XML nodes are inserted or removed from the tree?

Or is there another direct solution?

All help is appreciated!

+3


source to share


1 answer


do not use the @ and ^ operators, interfaces are already referenced

first code:

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;

      

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

+4


source







All Articles