Logo New book: Delphi 2007 Handbook
My blog in online
Delphi tech support service: support.marcocantu.com
Google
  Web www.marcocantu.com

Menu for Development

Site Menu
Delphi 2007 Handbook
Mastering Borland Delphi 2005
Essential Pascal
Essential Delphi
Buy Books Online
Code Repository
Newsgroups
White Papers
Tools
Conferences
Training
Delphi Links
Contact Marco

My Other Sites
Italian Site (www.marcocantu.it)
Developers Newsgroups Browser (dev.newswhat.com)
My town (www.piazzacavalli.net)
the delphi search
Wintech Italia (my company)

Breaking News
Buy Mastering Borland Delphi 2005 from Amazon
Free ebook: Mastering Delphi Update for Delphi 2006

Advertising
Home My Blog Books My Bookstore Development Links Marco


Home: Code Repository: Mastering Delphi 6

Chapter 23 - Project XmlDomTree

Project Structure

XmlDomTree.dpr
program XmlDomTree;

uses
  Forms,
  DomTreeForm in 'DomTreeForm.pas' {FormXmlTree};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TFormXmlTree, FormXmlTree);
  Application.Run;
end.
DomTreeForm.pas
unit DomTreeForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc, ComCtrls, ExtCtrls;

type
  TFormXmlTree = class(TForm)
    XMLDocument1: TXMLDocument;
    btnLoad: TButton;
    OpenDialog1: TOpenDialog;
    TreeView1: TTreeView;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure btnLoadClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    procedure DomToTree(XmlNode: IXMLNode; TreeNode: TTreeNode);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormXmlTree: TFormXmlTree;

implementation

{$R *.dfm}

procedure TFormXmlTree.btnLoadClick(Sender: TObject);
begin
  OpenDialog1.InitialDir := ExtractFilePath (Application.ExeName);
  if OpenDialog1.Execute then
  begin
    XMLDocument1.LoadFromFile(OpenDialog1.FileName);
    Treeview1.Items.Clear;
    DomToTree (XMLDocument1.DocumentElement, nil);
    TreeView1.FullExpand;
  end;
end;

procedure TFormXmlTree.DomToTree (XmlNode: IXMLNode; TreeNode: TTreeNode);
var
  I: Integer;
  NewTreeNode: TTreeNode;
  NodeText: string;
  AttrNode: IXMLNode;
begin
  // skip text nodes and other special cases
  if not (XmlNode.NodeType = ntElement) then
    Exit;
  // add the node itself
  NodeText := XmlNode.NodeName;
  if XmlNode.IsTextElement then
    NodeText := NodeText + ' = ' + XmlNode.NodeValue;
  NewTreeNode := TreeView1.Items.AddChild(TreeNode, NodeText);
  // add attributes
  for I := 0 to xmlNode.AttributeNodes.Count - 1 do
  begin
    AttrNode := xmlNode.AttributeNodes.Nodes[I];
    TreeView1.Items.AddChild(NewTreeNode,
      '[' + AttrNode.NodeName + ' = "' + AttrNode.Text + '"]');
  end;
  // add each child node
  if XmlNode.HasChildNodes then
    for I := 0 to xmlNode.ChildNodes.Count - 1 do
      DomToTree (xmlNode.ChildNodes.Nodes [I], NewTreeNode);
end;

procedure TFormXmlTree.Button1Click(Sender: TObject);
begin
  ShowMessage (XMLDocument1.DocumentElement.
    Attributes ['text']);
//  AttributeNodes.Nodes[0].NodeValue);
end;

procedure TFormXmlTree.Button2Click(Sender: TObject);
begin
  ShowMessage (XMLDocument1.DocumentElement.
    ChildNodes.Nodes[1].ChildValues['author']);
end;

procedure TFormXmlTree.Button3Click(Sender: TObject);
begin
  // this call creates an error with an odd essage
  ShowMessage (XMLDocument1.DocumentElement.Attributes ['test']);
end;

end.
DomTreeForm.dfm
object FormXmlTree: TFormXmlTree
  Left = 192
  Top = 107
  Width = 465
  Height = 401
  Caption = 'XmlDomTree'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object TreeView1: TTreeView
    Left = 0
    Top = 41
    Width = 457
    Height = 333
    Align = alClient
    Indent = 19
    TabOrder = 0
  end
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 457
    Height = 41
    Align = alTop
    TabOrder = 1
    object btnLoad: TButton
      Left = 16
      Top = 8
      Width = 75
      Height = 25
      Caption = 'Load'
      TabOrder = 0
      OnClick = btnLoadClick
    end
    object Button1: TButton
      Left = 96
      Top = 8
      Width = 75
      Height = 25
      Caption = 'Root Attr'
      TabOrder = 1
      OnClick = Button1Click
    end
    object Button2: TButton
      Left = 180
      Top = 8
      Width = 75
      Height = 25
      Caption = 'First Author'
      TabOrder = 2
      OnClick = Button2Click
    end
    object Button3: TButton
      Left = 264
      Top = 8
      Width = 75
      Height = 25
      Caption = 'Error'
      TabOrder = 3
      OnClick = Button3Click
    end
  end
  object XMLDocument1: TXMLDocument
    Left = 40
    Top = 40
    DOMVendorDesc = 'MSXML'
  end
  object OpenDialog1: TOpenDialog
    Left = 40
    Top = 96
  end
end