title: xml file validation with in-memory schema in xerces c++
body: <pre><code>#include &lt;xercesc/framework/Wrapper4InputSource.hpp&gt;
  #include &lt;xercesc/util/XMLString.hpp&gt;
  #include &lt;xercesc/sax2/SAX2XMLReader.hpp&gt;
  #include &lt;xercesc/parsers/XercesDOMParser.hpp&gt;
  #include &lt;xercesc/dom/DOMLSParser.hpp&gt;
  #include &lt;xercesc/dom/DOMImplementation.hpp&gt;
  #include &lt;xercesc/sax2/XMLReaderFactory.hpp&gt;
  #include &lt;xercesc/framework/MemBufInputSource.hpp&gt;
  #include &lt;xercesc/framework/LocalFileInputSource.hpp&gt;
  #include &lt;xercesc/sax/ErrorHandler.hpp&gt;
  #include &lt;xercesc/sax/SAXParseException.hpp&gt;
  #include &lt;xercesc/sax/Parser.hpp&gt;
  #include &lt;xercesc/validators/common/Grammar.hpp&gt;
  
  class CErrorHandler : public xercesc::DefaultHandler 
  {
  public:    
  CErrorHandler();
  
  virtual ~CErrorHandler();
  
  void startElement(const   XMLCh* const    uri,
                    const   XMLCh* const    localname,
                    const   XMLCh* const    qname,
                    const   XERCES_CPP_NAMESPACE::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 xercesc::SAXParseException&amp;);
  };
  
     CErrorHandler::CErrorHandler()
     {
     }
  
     CErrorHandler::~CErrorHandler()
     {
     }
  
     void CErrorHandler::startElement(const   XMLCh* const    uri,
                                      const   XMLCh* const    localname,
                                      const   XMLCh* const    qname,
                                      const   xercesc::Attributes&amp;     attrs)
     {
        char* name = xercesc::XMLString::transcode(localname);
        std::cout &lt;&lt; name;
        xercesc::XMLString::release(&amp;name);
     }
  
     void CErrorHandler::endElement(const   XMLCh* const    uri,
                                    const   XMLCh* const    localname,
                                    const   XMLCh* const    qname)
     {
        char* name = xercesc::XMLString::transcode(localname);
        xercesc::XMLString::release(&amp;name);
     }
  
     void CErrorHandler::fatalError(const xercesc::SAXParseException&amp; exception)
     {
        char* message = xercesc::XMLString::transcode(exception.getMessage());
        std::cout &lt;&lt; "Error: " &lt;&lt; message &lt;&lt; " at line: " &lt;&lt; exception.getLineNumber() &lt;&lt; std::endl;
        xercesc::XMLString::release(&amp;message);
     }
  
     void CErrorHandler::characters(const   XMLCh* const    chars,
                                    const   XMLSize_t       length
     )
     {
     }
  
  bool validateSchema()
  {
  
  std::string XSD_SHEMA ="&lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?&gt;...";   
  
  try 
     {
        xercesc::XMLPlatformUtils::Initialize();
     }
  
     catch (const  XERCES_CPP_NAMESPACE::XMLException&amp; toCatch) 
     {
        char* message =  xercesc::XMLString::transcode(toCatch.getMessage());
        std::cout &lt;&lt; "Error during initialization!" &lt;&lt; std::endl;
        std::cout &lt;&lt; "Exception message is: " &lt;&lt; message;
        XERCES_CPP_NAMESPACE::XMLString::release(&amp;message);
        return false;
     }
  
     xercesc::SAX2XMLReader* parser =  xercesc::XMLReaderFactory::createXMLReader();
     parser-&gt;setFeature( xercesc::XMLUni::fgSAX2CoreValidation, true);
     parser-&gt;setFeature( xercesc::XMLUni::fgSAX2CoreNameSpaces, true);
  
     xercesc::DefaultHandler* defaultHandler = new CErrorHandler();
     xercesc::ContentHandler* h  = new CErrorHandler();
  
     xercesc::MemBufInputSource mis(reinterpret_cast&lt; const XMLByte* &gt;(XSD_SHEMA.c_str() ), XSD_SHEMA.size (), "/schema.xsd");
     xercesc::Wrapper4InputSource wmis (&amp;mis, false);
  
     parser-&gt;loadGrammar (&amp;wmis, xercesc::Grammar::SchemaGrammarType, true);
     parser-&gt;setFeature (xercesc::XMLUni::fgXercesUseCachedGrammarInParse, true);
  
     void* id = (void*)("file:///schema.xsd");
     parser-&gt;setProperty (xercesc::XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, id);
  
     parser-&gt;setContentHandler(h);
     parser-&gt;setErrorHandler(defaultHandler);
  
     try 
     {
        parser-&gt;parse(mXMLFilePath.c_str());
     }
  
     catch (const  xercesc::XMLException&amp; toCatch) 
     {
        char* message =  xercesc::XMLString::transcode(toCatch.getMessage());
        std::cout &lt;&lt; "Exception message is: "&lt;&lt; message &lt;&lt; std::endl;;
        xercesc::XMLString::release(&amp;message);
        return false;
     }
  
     catch (const  xercesc::SAXParseException&amp; toCatch) 
     {
        char* message =  xercesc::XMLString::transcode(toCatch.getMessage());
        std::cout &lt;&lt; "Exception message is: " &lt;&lt; message &lt;&lt; std::endl;;
        xercesc::XMLString::release(&amp;message);
        return false;
     }
  
     catch (...) 
     {
        std::cout &lt;&lt; "Unexpected Exception" ;
        return false;
     }
  
     delete parser;
     delete defaultHandler;
     return true;
  }
  </code></pre>
  
  <p>I am trying to validate xml file with path mXMLFilePath and xsd schema in string XSD_SHEMA in c++ with Xerces lib. 
  I created CErrorHandler class and initialized it, set schema nolocation parameter for not setting in xml path to schema.
  It build`s, but not work. Have somebody any ideas? </p>