How To: Read XML in C++

// Program to get the current temperature from interceptvector.com
// Robert Burns, Diablo Valley College, Computer Science Department 4/21/04
// using Xerces version 2.4

#include <string>
using std::string;

#include <iostream>
using std::cout;
using std::endl;

#include <cstdlib>

#include <xercesc/parsers/SAXParser.hpp>
using xercesc_2_5::SAXParser;

#include <xercesc/sax/HandlerBase.hpp>
using xercesc_2_5::HandlerBase;

#include <xercesc/util/PlatformUtils.hpp>
using xercesc_2_5::AttributeList;
using xercesc_2_5::XMLException;
using xercesc_2_5::XMLPlatformUtils;
using xercesc_2_5::XMLString;

class MySaxHandler : public HandlerBase
{
  private:
  string temp; // degrees F
  string sky; // e.g, Mostly Sunny
  string city;
  string lastElement;

  public:
  void endDocument(){}
  void endElement(const XMLCh* const name){}
  void characters(const XMLCh* const chars, const unsigned int length)
  {
    if ((lastElement == "Temprature") && (temp == ""))
      temp = XMLString::transcode(chars);
    if ((lastElement == "Forecast") && (sky == ""))
      sky = XMLString::transcode(chars);
    if ((lastElement == "Location") && (city == ""))
      city = XMLString::transcode(chars);
  }
  void ignorableWhitespace(const XMLCh* const chars, const unsigned int length){}
  void processingInstruction(const XMLCh* const target, const XMLCh* const data){}
  void startDocument(){}
  void startElement(const XMLCh* const name, AttributeList& attributes)
  {
    lastElement = XMLString::transcode(name);
  }

  void notationDecl(const XMLCh* const name, const XMLCh* const publicId,
    const XMLCh* const systemId){}
  void unparsedEntityDecl(const XMLCh* const name, const XMLCh* const publicId, 
    const XMLCh* const systemId, const XMLCh* const notationName){}

  string getTemp() const;
};

int main()
{
  
  XMLPlatformUtils::Initialize();
  
  SAXParser* parser = new SAXParser;
  parser->setValidationScheme(SAXParser::Val_Auto);
  parser->setDoNamespaces(false);
  parser->setDoSchema(false);
  parser->setValidationSchemaFullChecking(false);

  try
  {
    MySaxHandler handler;
    parser->setDocumentHandler(&handler);
    parser->parse("http://www.ejse.com/WeatherService/Service.asmx/GetExtendedWeatherInfo?zipCode=94523");
    cout << handler.getTemp() << endl;
  }
  catch (const XMLException&)
  {
    XMLPlatformUtils::Terminate();
    return -1;
  }

  delete parser;
  XMLPlatformUtils::Terminate();
  return 0;
}

string MySaxHandler::getTemp() const
{
  return "In " + city + ", it's " + temp + " degrees Fahrenheit and " + sky + " skies";
}

/*
  Visual C++ Compilation Instructions
  ===================================
  1.  Go to http://xml.apache.org/dist/xerces-c/stable/, and download 
      xerces-c2_5_0-windows_nt-msvc_60.zip (or a newer version), and expand it 
      onto your hard drive (C:\). It will expand itself into folder 
      C:\xerces-c2_5_0-windows_nt-msvc_60 (or something similar).
  2.  In VC++6, Tools->Options->Directories, add full path 
      to the "C:\xerces-c2_5_0-windows_nt-msvc_60\include" folder.
  2a. In VC++NET, Tools->Options->Projects->VC++ Directories->Include Files->New Line,
      and enter into the new line: C:\xerces-c2_5_0-windows_nt-msvc_60\include
  3.  Copy the .dll's from  the "C:\xerces-c2_5_0-windows_nt-msvc_60\bin" folder
      to c:\windows folder
  4.  Create a project for your application.
  5.  In project, right-click "{project name} files", and select New Folder.
      Name it "Libs", and set the extension to "lib".
  6.  Add the .lib files from the "C:\xerces-c2_5_0-windows_nt-msvc_60\lib" folder to the project.
      They should appear in the new Libs folder.
  7.  Find documentation here: C:\xerces-c2_5_0-windows_nt-msvc_60\doc\html\apiDocs\index.html
*/