Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 4 - Delphi and XML
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Delphi and XML
By: David Web
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-11-03

    Table of Contents:
  • Delphi and XML
  • XSL example
  • XML Syntax
  • Using XML with Delphi

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Delphi and XML - Using XML with Delphi


    (Page 4 of 4 )

    Below is some code for a very simple program that enables you to open up and modify an XML document.


    unit Unit1;


    interface


    uses

    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

    Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls, ComCtrls;


    type

    TForm1 = class(TForm)

    xd: TXMLDocument;

    tv: TTreeView;

    Button1: TButton;

    Button2: TButton;

    Button3: TButton;

    Button4: TButton;

    od: TOpenDialog;

    Memo1: TMemo;

    procedure Button4Click(Sender: TObject);

    procedure Button1Click(Sender: TObject);

    procedure FormCreate(Sender: TObject);

    private

    { Private declarations }

    public

    { Public declarations }

    end;

    const

    IMG_NODE_ROOT = 0;

    IMG_NODE_CLOSED = 1;

    IMG_NODE_OPEN = 2;

    IMG_NODE_ROOT2 = 3;

    IMG_NODE_CLOSED2 = 4;

    IMG_NODE_OPEN2 = 5;

    var

    Form1: TForm1;


    implementation


    {$R *.dfm}


    procedure TForm1.Button4Click(Sender: TObject);

    begin

    close;

    end;


    procedure TForm1.Button1Click(Sender: TObject);

    begin

    memo1.Lines.LoadFromFile('myfeedsnew.xml');

    end;


    procedure TForm1.FormCreate(Sender: TObject);

    var

    rec:TSearchRec;

    SomeTxtFile : TextFile;

    buffer : string;

    begin

    // Try to find regular files matching Unit1.d* in the current dir

    if FindFirst('MyFeeds*.xml', faAnyFile, rec) = 0 then

    begin

    with tv.Items.AddFirst(nil, 'MyFeeds') do

    begin

    Selected:=true;

    {Set the roots image index}

    ImageIndex := IMG_NODE_ROOT;

    {Set the roots selected index. The same image is uses

    as for the ImageIndex}

    SelectedIndex := IMG_NODE_ROOT;

    end;


    repeat

    with tv.Items.AddChildFirst( tv.Selected, rec.Name) do

    begin

    {Set the image used when the node is not selected}

    ImageIndex := IMG_NODE_CLOSED;

    {Image used when the node is selected}

    SelectedIndex := IMG_NODE_OPEN;

    MakeVisible;

    end;


    until FindNext(rec) <> 0;


    // Must free up resources used by these successful finds

    FindClose(rec);

    end

    else begin

    with tv.Items.AddFirst( nil, 'No Files Found' ) do

    begin

    Selected := true;

    end;

    end;

    end;


    end.



    At this stage, the code is not complicated at all. It simply uses the loadfromfile() function to load an XML file and then displays it in the memo:

    procedure TForm1.Button1Click(Sender: TObject);

    begin

    memo1.Lines.LoadFromFile('myfeedsnew.xml');

    end;


    Once the XML file is loaded into the memo, you can make the changes that you want. The onformcreate procedure simply searches the myfeeds directory and then lists all the XML files that it finds in a treeview. It groups them together based on the directory to which they belong:


    procedure TForm1.FormCreate(Sender: TObject);

    var

    rec:TSearchRec;

    SomeTxtFile : TextFile;

    buffer : string;

    begin

    // Try to find regular files matching Unit1.d* in the current dir

    if FindFirst('MyFeeds*.xml', faAnyFile, rec) = 0 then

    begin

    with tv.Items.AddFirst(nil, 'MyFeeds') do

    begin

    Selected:=true;

    {Set the roots image index}

    ImageIndex := IMG_NODE_ROOT;

    {Set the roots selected index. The same image is uses

    as for the ImageIndex}

    SelectedIndex := IMG_NODE_ROOT;

    end;


    repeat

    with tv.Items.AddChildFirst( tv.Selected, rec.Name) do

    begin

    {Set the image used when the node is not selected}

    ImageIndex := IMG_NODE_CLOSED;

    {Image used when the node is selected}

    SelectedIndex := IMG_NODE_OPEN;

    MakeVisible;

    end;


    until FindNext(rec) <> 0;


    // Must free up resources used by these successful finds

    FindClose(rec);

    end

    else begin

    with tv.Items.AddFirst( nil, 'No Files Found' ) do

    begin

    Selected := true;

    end;

    end;

    end;


    end.

    In the next article we will discuss how to use DOM with Delphi. We will then improve on this application at that stage.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    DELPHI-KYLIX ARTICLES

    - Loading an XML Document into the DOM
    - Delphi Wrapper Classes and XML
    - Delphi and the DOM
    - Delphi and XML
    - Internet Access: Client Service
    - Finishing the Client for an Internet Access ...
    - The Client for an Internet Access Control Ap...
    - User Management for an Internet Access Contr...
    - Important Procedures for an Internet Access ...
    - Server Code for an Internet Access Control A...
    - Constructing the Interface for an Internet A...
    - Building a Server Application for an Interne...
    - Building an Internet Access Control Applicat...
    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek