XML RSS News Feed Reader with bare C++

A native C++ program that connects to an rss feed site and reads the XML title and description tags in a loop. We have used a bbc rss-xml feed in this progam to create a news ticker.
(Rev. 31-Oct-2024)

Categories | About |     |  

Parveen,

Table of Contents (top down ↓)

A native C++ program that connects to an rss feed site and reads the XML title and description tags in a loop. We have used a bbc rss-xml feed in this progam to create a news ticker.

This program is a bare bones program to get you started. It can be used for a minor project for Engineering BE, MCA (computer).

IMPORTANT NOTE: ONLY FOR WINDOWS. NEEDS A COMPILER WITH WINDOWS SET UP. COMPILE USING Visual Studio C++, Visual C++ Express, any edition, any version

The Plan

We shall first open an IStream to the rss-feed URL. The code will be the same as we have explained for Simple C, C++ Program to download a file from internet URL

Then we shall use the Win32 API called XmlLite to create an xml reader. The interface IXMLReader is obtained from a dll that is present on every consumer version of the Windows starting from Windows XP.

In its simplest form, the API CreateXmlReader takes 3 arguments as explained in the source code below. If the function succeeds, it returns S_OK, which is effectively, 0 cast as long. In other words, it returns a non-zero value on failure. The "if" condition is there to check just this. We do not expect any failure on a good pc. But should it fail try a different machine.

IXmlReader has a function called "Read" that returns the type of node read as one of the XmlNodeType enum values. The enumeration has about 11 or 12 possibilities, but we need only three of them. A switch is used to branch through the values of our interest. If we are at XmlNodeType_Element, the element is read and saved in "wchar_t* el". When the reader reads an XmlNodeType_Text or XmlNodeType_CDATA, we display the value to the console. For our current project we are interested only in the title and description tags.

Finally, both the COM interfaces, IStream and IXmlReader are released.

The Source Code

// ONLY FOR WINDOWS 
// NEEDS WINDOWS SET UP 
// COMPILE USING Visual Studio Express, 
// Visual C++ Express any edition 
// any version 
#pragma comment(lib, "xmllite.lib")

#pragma comment(lib, "urlmon.lib")

#include <urlmon.h>

#include <xmlLite.h>

#include <iostream>

#define getURL URLOpenBlockingStreamA

// only two tags to be read 
#define TITLE L"title"

#define DESCR L"description"

using namespace std;

int main()
{

  // Step 1 get the RSS XML Stream // 
  // Windows IStream interface 
  IStream* stream;

  // the address of the RSS feed 
  // we are using bbc, but you can 
  // use any other 
  const char* URL =
  "http://feeds.bbci.co.uk/"
  "news/world/rss.xml";

  cout << "Please wait. . . \r\n";

  // make a call to the URL 
  // a non-zero return means some error 
  if (getURL(0, URL, &stream, 0, 0))
  {

    cout << "Error occured.";

    cout << "Check internet";

    cout << "Check URL. Is it correct?";

    return -1;

  }

  // Step 2 create an XML reader // 
  IXmlReader* rdr;

  // windows API function that inits 
  // the IXMLReader interface so that 
  // we can read the XML tags in a loop 
  // takes 3 arguments. first is the 
  // interface identifier for the 
  // windows DLL present on your 
  // computer, the second is the pointer 
  // to the reader to be initialized 
  // the third argument is kept zero 
  if(CreateXmlReader(
  __uuidof(IXmlReader),
  reinterpret_cast<void**>(&rdr),
  0))
  {

    printf("Error. Try later");

    return -1;

  }

  rdr->SetInput(stream);

  // to support unicode text we use 
  // widechar instead of char 
  const wchar_t* el = 0;

  XmlNodeType nodeType;

  while (S_OK == rdr->Read(&nodeType))
  {

    switch (nodeType)
    {

    case XmlNodeType_Element:
      {

        // read the name of the element 
        // into el 
        rdr->GetLocalName(&el, 0);

      }

      break;

    case XmlNodeType_Text:
    case XmlNodeType_CDATA:
      {

        if(el)
        {

          const wchar_t* val = 0;

          // read the tag value 
          // inner text 
          rdr->GetValue(&val, 0);

          // if the element is the 
          // title or description tag, 
          // display it 
          if(!_wcsicmp(el, TITLE))
          {

            wcout << val << L"\r\n\r\n";

          }else if(!wcsicmp(el, DESCR))

          {

            wcout << val << "\r\n\r\n";

            wcout << L"***************";

            wcout << L"***************";

            wcout << L"***************";

            wcout << L"*******\r\n\r\n";

          }

          el = 0;

        }

      }

    default:
      break;

    }

  }

  stream->Release();

  rdr->Release();

  return 0;

}

The output

This is the copy pasted output. Some of it got truncated during copying, but it should give a fair idea of the whole thing.

Please wait. . .
BBC News - World

BBC News - World

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

BBC News - World

Germany shooting: 'Far-right extremist' carried out shisha bars attacks

Nine people are shot dead in two shisha bars in Hanau in a case being investigat
ed as terrorism.

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

UK says Russia's GRU behind massive Georgia cyber-attack

The UK Foreign Office said October's attack was designed to undermine Georgia's
sovereignty.

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

Coronavirus : South Korean sect identified as hotbed

Infections are linked to one woman, as South Korea confirms its first death from
 the covid-19 virus.

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

Coronavirus: Walking through Beijing's near-empty streets

The city is unrecognisable as people work from home and avoid going out.

*****************************. . .


This Blog Post/Article "XML RSS News Feed Reader with bare C++" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.