Dave thanks for your suggestion,

But it doesn't appear to be right -  let me show you:

I add a resource handler, and also a grammar in the following code.
However, the output remains the same

> DOM Error:no declaration found for element 'tx'
> DOM Error:attribute 'xmlns' is not declared for element 'tx'

Best regards (and truly confused!)
        Ben.

On 13 Mar 2009, at 17:29, David Bertoni wrote:

You're forcing the parser to validate your document, even though it has no grammar to use. As a result, it will report a validation error for every element in the document.

Dave

#include <iostream>

#include <xercesc/util/XercesDefs.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>

#include <xercesc/dom/DOMErrorHandler.hpp>
#include <xercesc/dom/DOMImplementationRegistry.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationLS.hpp>
#include <xercesc/dom/DOMLSParser.hpp>
#include <xercesc/dom/DOMLSInput.hpp>
#include <xercesc/dom/DOMLSResourceResolver.hpp>

#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/parsers/AbstractDOMParser.hpp>

using namespace xercesc;

class Rsr : public DOMLSResourceResolver {
public:
        DOMLSInput* Rsr::resolveResource(
                         const XMLCh* const resourceType,
                         const XMLCh* const namespaceUri,
                         const XMLCh* const publicId,
                         const XMLCh* const systemId,
                         const XMLCh* const baseURI ) {
                
                DOMLSInput* retval=NULL;
                if (namespaceUri) {
std::cerr << "Not found:" << (char *) (XMLString::transcode(namespaceUri)) << "\r";
                }
                return retval;
        }
};

class Err: public DOMErrorHandler {
public:
        bool Err::handleError(const xercesc::DOMError& domError) {
std::cerr << "DOM Error:" << (char *) (XMLString::transcode(domError.getMessage())) << "\r";
                return true;
        }
};

int main (int argc, char * const argv[]) {
        XMLPlatformUtils::Initialize();
        const XMLCh gLS[] = { 'L', 'S', chNull };
const XMLCh xfil[] = {'<','t','x',' ','x ','m ','l ','n ','s ','= ','"','h ','t ','t ','p ',':','/','/','b ','g','r','i','f','f','i','n','.','n','e','t','/','t','x','"',' ','/','>',chNull}; const XMLCh grammar[] ={'<','s','c','h','e','m','a',' ','x ','m ','l ','n ','s ','= ','"','h ','t ','t ','p ',':','/','/','w ','w ','w ','.','w ','3 ','.','o ','r ','g','/','2','0','0','1','/','X','M','L','S','c','h','e','m','a','"', ' ','t ','a ','r ','g ','e ','t ','N ','a ','m ','e ','s ','p ','a ','c ','e ','= ','"','h ','t ','t ','p ',':','/','/','b','g','r','i','f','f','i','n','.','n','e','t','"','>', '<','e','l','e','m','e','n','t',' ','n','a','m','e','=','"','t','x','"',' ','t ','y ','p ','e ','= ','"','a ','n ','y ','T ','y','p','e','"','/','>','<','/','s','c','h','e','m','a','>',chNull};

DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(gLS); DOMLSParser* parser = ((DOMImplementationLS*)impl)- >createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
        
        DOMConfiguration* dc = parser->getDomConfig();
        Err* errHandler = new Err();
        Rsr* rsrHandler = new Rsr();

        dc->setParameter(XMLUni::fgXercesSchema, true);
        dc->setParameter(XMLUni::fgXercesSchemaFullChecking, true);
        dc->setParameter(XMLUni::fgDOMDatatypeNormalization, true);
        dc->setParameter(XMLUni::fgDOMNamespaces, true);
        dc->setParameter(XMLUni::fgDOMValidate, true);

        dc->setParameter(XMLUni::fgDOMResourceResolver,rsrHandler);
        dc->setParameter(XMLUni::fgDOMErrorHandler,errHandler);
        
        DOMLSInput* ginput = ((DOMImplementationLS*)impl)->createLSInput();
        DOMLSInput* input = ((DOMImplementationLS*)impl)->createLSInput();
        
        ginput->setStringData(grammar);
ginput->setEncoding(XMLUni::fgXMLChEncodingString); //Alberto Massari's idea.

        input->setStringData((XMLCh*)xfil);
input->setEncoding(XMLUni::fgXMLChEncodingString); //Alberto Massari's idea.

        parser->loadGrammar(ginput, Grammar::SchemaGrammarType, true);

        DOMDocument* doc = parser->parse(input);
        input->release();
        ginput->release();
        parser->release();
        delete errHandler;
        delete rsrHandler;
        XMLPlatformUtils::Terminate();
    return 0;
}

Reply via email to