Thank you! This was really helpful.
What is still not ok, is to make the parsing in the
StringToDOMDocument(.) function do validation.
To do so, I did the following:
-) added an error handler
-) told the parser that it should do validation.
So I extended your code with the following piece of code (immediately
after the createLSParser(....) command:
============
if ( setParser )
{
if (xercesc::DOMConfiguration* domConfig = setParser->getDomConfig() )
{
// install an error handler
DOMTreeErrorReporter* errHandler = new DOMTreeErrorReporter;
domConfig->setParameter( xercesc::XMLUni::fgDOMErrorHandler,
errHandler );
// Make the parser validating,
domConfig->setParameter( xercesc::XMLUni::fgDOMValidate, true );
}
}
==============
where the error handler has reporting functions like:
=========================================
void DOMTreeErrorReporter::warning( const xercesc::SAXParseException& e )
{
std::cerr << "Warning at file \"" << e.getSystemId()
<< "\", line " << e.getLineNumber()
<< ", column " << e.getColumnNumber()
<< "\n Message: " << e.getMessage() << std::endl;
}
===========================================
The result is (when parsing an xml file)
Warning at file "0xffffffb2", line 0, column 140736376741424
Message: 0x2
Warning at file "0xffffffc1", line 0, column 140736376740896
Message: 0x2
Warning at file "0xffffffc1", line 0, column 140736376740896
Message: 0x2
Warning at file "0xffffffc1", line 0, column 140736376740896
Message: 0x2
Warning at file "0xffffffb2", line 0, column 140736376741424
Message: 0x2
Warning at file "0xffffffb2", line 0, column 140736376741424
Message: 0x2
Warning at file "0xffffffb2", line 0, column 140736376741424
Message: 0x2
Warning at file "0xffffffb2", line 0, column 140736376741424
Message: 0x2
This obviously is bogus. What is wrong?
Ricardo Mendes wrote:
> DOMDocument* setParser::StringToDOMDocument(char* xmldoc)
> {
> DOMDocument* xmlDoc;
>
> static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
> DOMImplementation* impl =
> DOMImplementationRegistry::getDOMImplementation(gLS);
> DOMLSInput* input = impl->createLSInput();
> DOMLSParser* setParser =
> ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS,
> 0);
>
> XMLString::trim(xmldoc);
> XMLCh* doc = XMLString::transcode(xmldoc);
> input->setStringData(doc);
>
> xmlDoc = setParser->parse(input);
> xmlDoc->normalize();
>
> XMLString::release( &doc );
>
> return xmlDoc;
> }
>
> On Fri, Jul 9, 2010 at 12:31 PM, Ricardo Mendes <[email protected]> wrote:
>
>> Hi,
>>
>> you can do for instance:
>>
>> char* setParser::DOMDocumentToString(DOMDocument* xmlDoc)
>> {
>> const XMLCh gLS[] = { 'L', 'S', chNull };
>> DOMImplementation* impl =
>> DOMImplementationRegistry::getDOMImplementation(gLS);
>> DOMLSSerializer* serializer =
>> ((DOMImplementationLS*)impl)->createLSSerializer();
>> DOMLSOutput* output = ((DOMImplementationLS*)impl)->createLSOutput();
>> output->setEncoding(XMLUni::fgUTF8EncodingString);
>> MemBufFormatTarget* mbft = new MemBufFormatTarget;
>> output->setByteStream(mbft);
>> serializer->write(xmlDoc,output);
>> output->release();
>> serializer->release();
>>
>> return (char*)mbft->getRawBuffer();
>> }
>>
>> On Thu, Jul 8, 2010 at 11:04 PM, B.W.H. van Beest <[email protected]> wrote:
>>
>>> Hi,
>>>
>>> Can somebody please explain to me in some detail what I should do if I
>>> want to serialise a xercesc::DOMDocument,
>>> with the xml-output going to some memory buffer, then parse that xml
>>> buffer and validate it against a given schema?
>>>
>>> I read that the parser (XercesDOMParser or SAXParser) have a parse
>>> method "parse( const InputSource&)" method,
>>> and that there is a MemBufInputSource derivate class of InputSource, but
>>> how do I create an approppriate instance from a DOMDocument?
>>>
>>>
>>> Your help is greatly appreciated.
>>>
>>> Bertwim
>>>
>>>
>
>
>