You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by "Sharma, Gaurav" <Ga...@Safenet-inc.com> on 2010/12/20 08:19:51 UTC

Closing file handle for DOMLSOutput file

Hi All,

Can anyone tell me that how I can close the handle for output xml file that I open to write the document tree using the following code:

DOMImplementation *impl = 
DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("LS"));
   DOMLSSerializer *writer = ((DOMImplementationLS*)impl)->createLSSerializer();

   writer->getDomConfig()->setParameter(XMLString::transcode("format-pretty-print"), true);
      DOMLSOutput *output = ((DOMImplementationLS*)impl)->createLSOutput();
      XMLFormatTarget *my_form_target = new LocalFileFormatTarget(out_file_name);
      output->setByteStream(my_form_target);
      writer->write(doc, output);
      output->release();
      output = NULL;
   }
   writer->release();
   writer = NULL;

Even after releasing both writer and output I am not able to close the file handle opened by the process. I cant delete it and even I cant reparse it using XMLDomParser->parse() function.

Any help would be appreciated.

With Best Regards
Gaurav Sharma
The information contained in this electronic mail transmission 
may be privileged and confidential, and therefore, protected 
from disclosure. If you have received this communication in 
error, please notify us immediately by replying to this 
message and deleting it from your computer without copying 
or disclosing it.



Failure of XercesDOMParser->parse function

Posted by "Sharma, Gaurav" <Ga...@Safenet-inc.com>.
Hi All,


I am using XercesDOMParser to parse xml file with validation turn on. With following snippets of code

std::auto_ptr<xercesc::XercesDOMParser> parser(new xercesc::XercesDOMParser(0,
                                                                               xercesc::XMLPlatformUtils::fgMemoryManager,                                                                               grammar_pool));

   parser->setDoNamespaces(true);
   parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
   parser->setDoSchema(true);
   parser->setValidationSchemaFullChecking(true);
   parser->setIncludeIgnorableWhitespace(false);
   parser->setCreateCommentNodes(false);
   parser->cacheGrammarFromParse(true);
   parser->useCachedGrammarInParse(true);
   parser->setLoadSchema(false);
   parser->setHandleMultipleImports(true);
   parser->setErrorHandler(&errhandler);
   parser->parse(xml_buffer);
   if (parser->getErrorCount() > 0)
   {
   	//throw exception
   }

Where errhandler is object of the following class

class ClgnDOMErrorHandler : public xercesc::ErrorHandler
{
public:
   ClgnDOMErrorHandler()
   {}
   ~ClgnDOMErrorHandler()
   {}
   void ClgnDOMErrorHandler::warning(const xercesc::SAXParseException&)
   {
      // Ignore all warnings.
   }
   void error(const xercesc::SAXParseException& toCatch)
   {
	// Throw proper exception
   }
   void fatalError(const xercesc::SAXParseException& toCatch)
   {
	// Throw proper exception
   }
   void ClgnDOMErrorHandler::resetErrors()
   {

   }  
};

My problem is that if the input xml_buffer is not valid as per the schema(like and tag is missing or tag is un terminated) then it properly goes to errhandler's error function. But something weird is happening now and parse() function is failing and its not going to errhandlers' error() function and instead coming to   
if (parser->getErrorCount() > 0). Though this condition was put here to trap some unhandled error but now I am stuck how to know whats the error:)

Is there any way I can trap the exact error with parse() function.

Thanks & Regards
Gaurav Sharma

The information contained in this electronic mail transmission 
may be privileged and confidential, and therefore, protected 
from disclosure. If you have received this communication in 
error, please notify us immediately by replying to this 
message and deleting it from your computer without copying 
or disclosing it.



Re: Closing file handle for DOMLSOutput file

Posted by Alberto Massari <Al...@progress.com>.
The output file is closed when the LocalFileFormatTarget object is 
deleted; now you are leaking it, so this never occurs.
Try using

       LocalFileFormatTarget my_form_target(out_file_name);
       output->setByteStream(&my_form_target);


Alberto

On 12/20/2010 8:19 AM, Sharma, Gaurav wrote:
> Hi All,
>
> Can anyone tell me that how I can close the handle for output xml file that I open to write the document tree using the following code:
>
> DOMImplementation *impl =
> DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("LS"));
>     DOMLSSerializer *writer = ((DOMImplementationLS*)impl)->createLSSerializer();
>
>     writer->getDomConfig()->setParameter(XMLString::transcode("format-pretty-print"), true);
>        DOMLSOutput *output = ((DOMImplementationLS*)impl)->createLSOutput();
>        XMLFormatTarget *my_form_target = new LocalFileFormatTarget(out_file_name);
>        output->setByteStream(my_form_target);
>        writer->write(doc, output);
>        output->release();
>        output = NULL;
>     }
>     writer->release();
>     writer = NULL;
>
> Even after releasing both writer and output I am not able to close the file handle opened by the process. I cant delete it and even I cant reparse it using XMLDomParser->parse() function.
>
> Any help would be appreciated.
>
> With Best Regards
> Gaurav Sharma
> The information contained in this electronic mail transmission
> may be privileged and confidential, and therefore, protected
> from disclosure. If you have received this communication in
> error, please notify us immediately by replying to this
> message and deleting it from your computer without copying
> or disclosing it.
>
>
>
>