Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
499 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Easy Parsing an XML FileGo to Max Kleiner's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
How do you get the elements from an XML file ?
Product:
Delphi all versions
Category:
XML
Skill Level:
Scoring:
Last Update:
05/16/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet XML Parser DOM
Times Scored:
13
Visits:
8457
Uploader: Max Kleiner
Company: kleiner kommunikation
Reference: N/A
Component Download: http://max.kleiner.com/myconfig.xml
 
Question/Problem/Abstract:
In modern times, a configuration file has to be an XML standard so you want to parse that file to get the elements from corresponding nodes.
Answer:



First you have to import the Type library. This will create a wrapper class for that component and all you have to do is to name it in uses in your unit.
I used msxml.dll(Version 2.0) to install the XML parsing components in the IDE through the Import Type Library option.
See for more details:

Importing XML DOM Parser in Delphi /ID 2021


Second we produce a simple XML file like a configuration file:
(Name the file myconfig.xml)
(Strange things happen (cause the xml interpreter in d3k-editor) with a well-formed file after submit the article, so I had to cancel first tags between databases and databases )
please download the file: http://max.kleiner.com/myconfig.xml
******************************************************************************
?xml version="1.0"?>
{databases
  database db="InterBase"
     connection
       name>TrueSoft
       path>superPath
       user>MaxMin
    /connection>
   /database>
   database db="myMax">
     connection>
       name>maXml
       server>kylix02
       user>carpeDiem
    /connection>
   /database>
/databases>}
*****************************************************************************

Third we build the procedure that parses 3 elements and assign it to strings:

myname, mypath, myuser: string[255];

The accID and idNr are just to make the procedure more flexible for permissions and node navigation. The whole procedure goes like this:

1. Find the file myconfig.xml
2. Create the interface pointer xmlDoc
3. Load the XML file
4. Check the error handling with xmlDoc.parseError
5. Select the node
6. Check if it has childs
7. Get the nodelist and assign the elements to strings

*****************************************************************************

procedure TForm1.parseXML;
var
  SRec1: TSearchRec;
  accID: boolean;
  idNr: byte;
  myXMLpath: string;
  xmlDoc: IXMLDomDocument2;
  xmlnode: IXMLDomNode;
  xmlnodelist: IXMLDomNodeList;
  myname, mypath, myuser: string[255];
begin
  accID:= true;
  idNr:= 1;
  myXMLpath:= extractFilePath(Application.ExeName);
  if FindFirst (myXMLpath+'myconfig.xml',faAnyFile,SRec1)=0 then begin
    xmlDoc:= CoDomDocument30.Create;
    xmlDoc.validateOnParse := True;
    xmlDoc.async := False;
    xmlDoc.load(myXMLpath+'myconfig.xml');
    if xmlDoc.parseError.errorCode = 0 then begin
      if accID then begin
        xmlDoc.setProperty('SelectionLanguage','XPath');
        xmlnode := xmlDoc.selectSingleNode('//databases/database
                                         [@db='+''''+'InterBase'+''''+ ']');
        if xmlnode.hasChildNodes then begin
          if (idNR <= xmlnode.childNodes.length) and (idNR > 0) then begin
            xmlnodelist := xmlnode.childNodes.item[(idNR-1)].selectNodes
                                                                   ('name');
            if xmlnodelist.length <> 0 then
            myname:=xmlnode.childNodes.item[(idNR-1)].selectNodes('name').item
                                                                  [0].text;
            xmlnodelist := xmlnode.childNodes.item[(idNR-1)].selectNodes
                                                                   ('path');
            if xmlnodelist.length <> 0 then
            mypath:=xmlnode.childNodes.item[(idNR-1)].selectNodes('path').item
                                                                  [0].text;
            xmlnodelist := xmlnode.childNodes.item[(idNR-1)].selectNodes
                                                                   ('user');
            if xmlnodelist.length <> 0 then
            myuser:=xmlnode.childNodes.item[(idNR-1)].selectNodes('user').item
                                                                  [0].text;
          end;
        end else
        showmessage('no XML Childs');
      end else
      showmessage('no permission');
    end else begin
      showmessage('load XML error');
      xmlDoc := NIL;
      FindClose(SRec1)
    end;
  end else
  showmessage('no XML file');
end;

Carpe Diem...





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Shkolnik
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)