title: XML SAX parser using xerces in c++
body: <pre><code>    #include &lt;xercesc/sax2/SAX2XMLReader.hpp&gt;
  #include &lt;xercesc/sax2/XMLReaderFactory.hpp&gt;
  #include &lt;xercesc/sax2/DefaultHandler.hpp&gt;
  #include &lt;xercesc/util/XMLString.hpp&gt;
  #include &lt;conio.h&gt;
  #include &lt;xercesc/parsers/SAXParser.hpp&gt;
  #include &lt;xercesc/sax/HandlerBase.hpp&gt;
  #include &lt;xercesc/util/XMLString.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;xercesc/dom/DOM.hpp&gt;
  #include &lt;xercesc/parsers/XercesDOMParser.hpp&gt;
  using namespace std;
  using namespace xercesc;
  
  
  class ErnstSax2Handler : public DefaultHandler 
  {
  public:
  ErnstSax2Handler(void);
  virtual ~ErnstSax2Handler(void);
  
  void startElement(
      const   XMLCh* const    uri,
      const   XMLCh* const    localname,
      const   XMLCh* const    qname,
      const   Attributes&amp;     attrs
  );
  void endElement(
      const   XMLCh* const    uri,
      const   XMLCh* const    localname,
      const   XMLCh* const    qname
  );
  void characters(
      const   XMLCh* const    chars,
      const   XMLSize_t       length
  );
  
  void fatalError(const SAXParseException&amp;);
  
  protected:
  // define variables to save the state
  };
  
  void ErnstSax2Handler::startElement(const   XMLCh* const    uri,
                          const   XMLCh* const    localname,
                          const   XMLCh* const    qname,
                          const   Attributes&amp;     attrs)
  {
  char* name = XMLString::transcode(localname);
  cout&lt;&lt;name;
  // ...
  XMLString::release(&amp;name);
  }
  void ErnstSax2Handler::endElement(
                  const   XMLCh* const    uri,
                  const   XMLCh* const    localname,
                  const   XMLCh* const    qname)
  {
  char* name = XMLString::transcode(localname);
  
  //...
  XMLString::release(&amp;name);
  }
  void ErnstSax2Handler::fatalError(const SAXParseException&amp; exception)
  {
  char* message = XMLString::transcode(exception.getMessage());
  cout &lt;&lt; "Fatal Error: " &lt;&lt; message
       &lt;&lt; " at line: " &lt;&lt; exception.getLineNumber()
       &lt;&lt; endl;
  XMLString::release(&amp;message);
  }
  void ErnstSax2Handler::characters(
  const   XMLCh* const    chars,
  const   XMLSize_t       length
  )
  {
   // This is called when the parser is reading text.
   // You will need to save what state you are in via
   // startElement / endElement.
   }
  
  
  int main () {
  
      try {
          XMLPlatformUtils::Initialize();
      }
      catch (const XMLException&amp; toCatch) {
          char* message = XMLString::transcode(toCatch.getMessage());
          cout &lt;&lt; "Error during initialization! :\n";
          cout &lt;&lt; "Exception message is: \n"
               &lt;&lt; message &lt;&lt; "\n";
          XMLString::release(&amp;message);
          return 1;
      }
  
      char* xmlFile = "test.xml";
      SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
      parser-&gt;setFeature(XMLUni::fgSAX2CoreValidation, true);
      parser-&gt;setFeature(XMLUni::fgSAX2CoreNameSpaces, true);   // optional
  
      DefaultHandler* defaultHandler = new DefaultHandler();
      xercesc::ContentHandler* h  = new DefaultHandler();
  
      parser-&gt;setContentHandler(h);
      parser-&gt;setErrorHandler(defaultHandler);
  
      try {
          parser-&gt;parse(xmlFile);
      }
      catch (const XMLException&amp; toCatch) {
          char* message = XMLString::transcode(toCatch.getMessage());
          cout &lt;&lt; "Exception message is: \n"
               &lt;&lt; message &lt;&lt; "\n";
          XMLString::release(&amp;message);
          return -1;
      }
      catch (const SAXParseException&amp; toCatch) {
          char* message = XMLString::transcode(toCatch.getMessage());
          cout &lt;&lt; "Exception message is: \n"
               &lt;&lt; message &lt;&lt; "\n";
          XMLString::release(&amp;message);
          return -1;
      }
      catch (...) {
          cout &lt;&lt; "Unexpected Exception \n" ;
          return -1;
      }
  
  
      getch();
      delete parser;
      delete defaultHandler;
      return 0;
  }
  </code></pre>
  
  <p>When I run above code in visual studio 2010, it won't print any element name and their content on the screen. Can anybody help me how to call any handler and how to get content from elements(nodes)?
  Thanx in advance.</p>