Reading and Manipulating XML files (RSS feeds) with Delphi

of 04

Blog? Syndication?

Depending upon who you talk to, a blog is a personal Web diary, a collection of short, dated discussions with commentary, or a way of publishing news and information. Well, the About Delphi Programming Home page acts as a blog.

The Stay Up-To-Date page hosts the link to the the XML file that can be used for Really Simple Syndication (RSS).

About Delphi Programming Blog Feed

Now about parsing the XML file that lists the latest additions to this site.

Here are the basics of the About Delphi Programming RSS:

  1. It is XML. This means it must be well-formed, include a prolog and DTD, and all elements must be closed.
  2. The first element in the document is the element. This includes a mandatory version attribute.
  3. The next element is the element. This is the main container for all RSS data.
  4. The element is the title, either of the entire site (if it's at the top) or of the current item (if it's within an ).
  5. The element indicates the URL of the Web page that corresponds to the RSS feed, or if it's within an , the URL to that item.
  6. The element describes the RSS feed or the item.
  7. The element is the meat of the feed. These are all the headlines (), URL () and description () that will be in your feed.
of 04

The TXMLDocument Component

To be able to display the latest headlines inside a Delphi project, you first need to download the XML file. Since this XML file is updated on a day by day basic (new entries added) you'll need code designed to save the contents of a specified URL to a file.

The TXMLDocument component

In general, here are the steps that describe how to use TXMLDocument:

  1. Add a TXMLDocument component to your form.
  2. If the XML document is stored in a file, set the FileName property to the name of that file.
  3. Set the Active property to True.
  4. The data XML represents is available as a hierarchy of nodes. Use methods designed to return and work with a node in an XML document (like ChildNodes.First).
of 04

Parsing XML, Delphi way

Create a new Delphi project and drop a TListView (Name: 'LV') component on a form. Add a TButton (Name : 'btnRefresh') and a TXMLDocument (Name : 'XMLDoc'). Next, add three columns to the ListView component (Title, Link and Description). Finally, add the code to download the XML file, parse it with TXMLDocument and display inside the ListView in the button's OnClick event handler.

Below you can find the portion of that code.

   StartItemNode : IXMLNode;
   ANode : IXMLNode;
   STitle, sDesc, sLink : WideString;
  //points to local XML file in "original" code
  XMLDoc.FileName := '';
  StartItemNode := XMLDoc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('item') ;
  ANode := StartItemNode;
    STitle := ANode.ChildNodes['title'].Text;
    sLink := ANode.ChildNodes['link'].Text;
    sDesc := ANode.ChildNodes['description'].Text;
    //add to list view
    with LV.Items.Add do
      Caption := STitle;
      SubItems.Add(sLink) ;
    ANode := ANode.NextSibling;
  until ANode = nil;

of 04

Full Source Code

I suppose the code is more or less easy to understand:

  1. Make sure FileName property of the TXMLDocument points to our XML file.
  2. Set Active to True
  3. Find the first ("meat") node
  4. Iterate through all the nodes and grab the information they cary.
  5. Add each node's value to ListView

Maybe only the next line can be confusing: StartItemNode := XMLDoc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('item') ;

The DocumentElement property of the XMLDoc provides access to the root node of the document. This root node is the element. Next, ChildNodes.First returns the only child node to the element, which is the node. Now, ChildNodes.FindNode('item') finds the first "meat" node. Once we have the first node we simply iterate through all the "meat" nodes in the document. The NextSibling method returns the next child of a node’s parent.

That's it. Make sure you download the full source. And of course, feel free and encouraged to post any comments to this article on our Delphi Programming Forum.