Hi, 

   I wrote my own errorhandler class to catch the errors that occur if the 
XML file is not valid. 

   For some reason it never throws out any errors, no matter even if the 
schema does not exists and the XML file is invalid. 

   I have attached the code. Could use some help. 

Thanks
Suchit. 
//////////////////////////////////
//Author: Suchit Batheja        //
//Date  : July 14th 2003        //
//////////////////////////////////  
 

// ---------------------------------------------------------------------------
//  Includes for all the program files to see
// ---------------------------------------------------------------------------

#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp> 
#include <xercesc/sax2/DefaultHandler.hpp> 
#include <xercesc/util/XMLString.hpp> 
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/sax2/Attributes.hpp>
#include <xercesc/util/PanicHandler.hpp>
#include <xercesc/util/XMLUni.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/sax/SAXException.hpp>

XERCES_CPP_NAMESPACE_USE
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
using namespace xercesc; 
  
// ---------------------------------------------------------------------------
//  This is a simple class that lets us do easy (though not terribly efficient)
//  trancoding of XMLCh data to local code page for display.
// ---------------------------------------------------------------------------
class StrX
{
public :
    // -----------------------------------------------------------------------
    //  Constructors and Destructor
    // -----------------------------------------------------------------------
    StrX(const XMLCh* const toTranscode)
    {
        // Call the private transcoding method
        fLocalForm = XMLString::transcode(toTranscode);
    }
 
    ~StrX()
    {
        XMLString::release(&fLocalForm);
    }
 
    // -----------------------------------------------------------------------
    //  Getter methods
    // -----------------------------------------------------------------------
    const char* localForm() const
    {
        return fLocalForm;
    }

private :
    // -----------------------------------------------------------------------
    //  Private data members
    //
    //  fLocalForm
    //      This is the local code page form of the string.
    // -----------------------------------------------------------------------
    char*   fLocalForm;
};

inline ostream& operator<<(ostream& target, const StrX& toDump)
{
    target << toDump.localForm();
    return target;
}
 
///////////////////////////////////////////////////////////////////////////
//MyErrorHandler Class. Inherited from DefaultHandler class              //
///////////////////////////////////////////////////////////////////////////

class MyErrorHandler : public DefaultHandler
{
public:
    // -----------------------------------------------------------------------
    //  Constructors and Destructor
    // -----------------------------------------------------------------------
    MyErrorHandler();
    ~MyErrorHandler();
 
 
    // -----------------------------------------------------------------------
    //  Getter methods
    // -----------------------------------------------------------------------
 
    bool getSawErrors() const
    {
     return fSawErrors;
    } 
 
    // -----------------------------------------------------------------------
    //  Handlers for the SAX ContentHandler interface
    // -----------------------------------------------------------------------
  /*  void startElement(const XMLCh* const uri, const XMLCh* const localname, const 
XMLCh* const qname, const Attributes& attrs);
    void characters(const XMLCh* const chars, const unsigned int length);
    void ignorableWhitespace(const XMLCh* const chars, const unsigned int length);
    void resetDocument();
*/
    // -----------------------------------------------------------------------
    //  Handlers for the SAX ErrorHandler interface
    // -----------------------------------------------------------------------

    void warning(const SAXParseException& exception);
    void error(const SAXParseException& exception);
    void fatalError(const SAXParseException& exception);
    void resetErrors();
 
 
private:
    // -----------------------------------------------------------------------
    //  Private data members
    //
    //  fSawErrors
    //      This is set by the error handlers, and is queryable later to
    //      see if any errors occured.
    // -----------------------------------------------------------------------
    bool            fSawErrors;
};

//----------------------------------------------------------------------------
//Implementation of MyErrorHandler Class                                     
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// Constructors and Destructor
// ---------------------------------------------------------------------------
MyErrorHandler::MyErrorHandler():fSawErrors(false)
{
}
MyErrorHandler::~MyErrorHandler()
{
}

// ---------------------------------------------------------------------------
//  MyErrorHandler: Implementation of the SAX DocumentHandler interface
// ---------------------------------------------------------------------------
/*void MyErrorHandler::startElement(const XMLCh* const uri
                                   , const XMLCh* const localname
                                   , const XMLCh* const qname
                                   , const Attributes& attrs)
{
}
 
void MyErrorHandler::characters(  const   XMLCh* const    char, const unsigned int    
length)
{
   
}
 
void MyErrorHandler::ignorableWhitespace( const   XMLCh* const chars, const unsigned 
int length)
{
  
}
 
void MyErrorHandler::resetDocument()
{
  fSawErrors = false;
}
*/
// ---------------------------------------------------------------------------
//  MyErrorHandler: Overrides of the SAX ErrorHandler interface
// ---------------------------------------------------------------------------
void MyErrorHandler::error(const SAXParseException& e)
{
    fSawErrors = true;
    cerr << "\nError at file " << StrX(e.getSystemId())
                 << ", line " << e.getLineNumber()
                 << ", char " << e.getColumnNumber()
         << "\n  Message: " << StrX(e.getMessage()) << endl;
}
 
void MyErrorHandler::fatalError(const SAXParseException& e)
{
    fSawErrors = true;
    cerr << "\nFatal Error at file " << StrX(e.getSystemId())
                 << ", line " << e.getLineNumber()
                 << ", char " << e.getColumnNumber()
         << "\n  Message: " << StrX(e.getMessage()) << endl;
}
 
void MyErrorHandler::warning(const SAXParseException& e)
{
    cerr << "\nWarning at file " << StrX(e.getSystemId())
                 << ", line " << e.getLineNumber()
                 << ", char " << e.getColumnNumber()
         << "\n  Message: " << StrX(e.getMessage()) << endl;
}
 
void MyErrorHandler::resetErrors()
{
    fSawErrors = false;
}

/////////////////////////////////////////////////////
//Main program                                     //
/////////////////////////////////////////////////////
int main (int argc, char* args[]) 
{ 
  try { 
       XMLPlatformUtils::Initialize(); 
  } 
  catch (const XMLException& toCatch) { 
    char* message = XMLString::transcode(toCatch.getMessage()); 
    cout << "Error during initialization! :\n"; 
    cout << "Exception message is: \n" << message << "\n"; 
    XMLString::release(&message); 
    return 1; 
   }

  char* xmlFile = "my_xmlfile.xml"; 
  SAX2XMLReader::ValSchemes    valScheme    = SAX2XMLReader::Val_Always;  
  SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
  XMLCh* propertyValue = XMLString::transcode("my_schema.xsd"); 
//   ArrayJanitor<XMLCh> janValue(propertyValue); 
   parser->setProperty(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, 
propertyValue);
  //DefaultHandler* defaultHandler = new DefaultHandler(); 
  MyErrorHandler* defaultHandler = new MyErrorHandler();
  parser->setContentHandler(defaultHandler); 
  parser->setErrorHandler(defaultHandler);
  try { 
    parser->parse(xmlFile); 
  }  
  catch (const XMLException& toCatch) { 
    char* message = XMLString::transcode(toCatch.getMessage()); 
    cout << "Exception message from first catch is: \n" << message << "\n"; 
    XMLString::release(&message); 
    return -1; 
  } 
/*  catch (const SAXParseException& toCatch) { 
    char* message = XMLString::transcode(toCatch.getMessage()); 
    cout << "Exception message from second catch is : \n" << message << "\n"; 
    XMLString::release(&message); 
    return -1; 
   } */
  catch (...) { 
   cout << "Unexpected Exception \n" ; 
   return -1; 
   } 
   delete parser; 
   delete defaultHandler; 
   return 0; 
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to