faqts : Computers : Programming : Languages : Delphi : XML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

31 of 37 people (84%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Delphi: XML: Parser: How to create a simple XML parser in Delphi?

Oct 9th, 2003 20:37
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 24 August 2003 - 11:54 pm ---------------------

Delphi: XML: Parser: How to create a simple XML parser in Delphi?


Delphi v7:


Steps: Overview:

 1. -Open a new application in Delphi

http://www.faqts.com/knowledge_base/view.phtml/aid/23924/fid/175

 2. -Put a button on your form

http://www.faqts.com/knowledge_base/view.phtml/aid/23926/fid/175

 3. -Put an XMLDocument component on your form

http://www.faqts.com/knowledge_base/view.phtml/aid/23929/fid/175

 4. -Change its properties:

http://www.faqts.com/knowledge_base/view.phtml/aid/23931/fid/175

     1. set property:

         'Active' = true

     2. possibly set the property  DOMVendor
        (the default is 'MSXML')

     3. set property

         'filename' = <your XML filename>

          (e.g. 'c:\temp\book.xml')

 5. -Double click on the button, and fill in the following
     source code:

procedure TForm1.Button1Click(Sender: TObject);
var node : IXMLNode;
var nodes : IXMLNodeList;
var I : integer;
begin
 node := XMLDocument1.DocumentElement;
 ShowMessage( node.NodeName );
 nodes := node.ChildNodes;
 if node.HasChildNodes then begin
  for I := 0 to nodes.Count - 1 do begin
   ShowMessage( 'Node( ' + IntToStr( I ) + ' ) is ' + nodes[ 
I ].NodeName );
  end;
 end;
end;

---

 6. Running this code will show you the main nodes.

---

For example, if this is your XML file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE MYBOOKS SYSTEM "book.dtd">
<?xml-stylesheet type="text/xsl" href="book.xsl"?>
<!-- library: (filenamemacro=book.xml)  -->
<MYBOOKS>
 <BOOK>
  <AUTHOR>
    Holzner, Steve
  </AUTHOR>
  <TITLE>
    Inside XML
  </TITLE>
 </BOOK>
 <BOOK>
  <AUTHOR>
    Knuth, Donald
  </AUTHOR>
  <TITLE>
    the art of computer programming
  </TITLE>
 </BOOK>
</MYBOOKS>

---

You will first see:

 'MYBOOKS'

then

 Node 0 is 'BOOK'

then

 Node 1 is 'BOOK'

---

Internet: see also:

---

What is XML?
http://www.faqts.com/knowledge_base/view.phtml/aid/6640

---

Basic XML Parsing in Delphi
http://homepages.borland.com/ccalvert/TechPapers/Delphi/XMLSimple/XMLSi
mple.html

---

About XML and Delphi
http://delphi.about.com/library/weekly/aa072500a.htm

----------------------------------------------------------------------