[xsd-users] File Closing

Prather, Ryan C SSgt DMSG/WMTS Ryan.Prather at afspc.af.mil
Mon Jul 23 13:02:15 EDT 2007


Ok, Boris, that seemed to work great.  Like you were mentioning we are
getting a validation error now.

"Test.xml:1:1 Invalid Document Structure"

I think that fix that you offered would fix it.  Thanks.

Ryan

-----Original Message-----
From: Boris Kolpackov [mailto:boris at codesynthesis.com] 
Sent: Monday, July 16, 2007 10:10 AM
To: Prather, Ryan C SSgt DMSG/WMTS
Cc: xsd-users at codesynthesis.com
Subject: Re: [xsd-users] File Closing

Hi Ryan,

Prather, Ryan C SSgt DMSG/WMTS <Ryan.Prather at afspc.af.mil> writes:

> Is xsd/xerces thread-safe?

Yes but you need to make sure the Xerces-C++ runtime if initialized and
terminated only once. The easies way to do this is to
initialize/terminate Xerces-C++ from main() when there aren't any
threads yet/anymore:

#include <xercesc/util/PlatformUtils.hpp>

int
main ()
{
  xercesc::XMLPlatformUtils::Initialize ();

  {
    // Start threads and parse here.
  }

  xercesc::XMLPlatformUtils::Terminate (); }

Because you initialize the runtime yourself you should also pass the
xml_schema::flags::dont_initialize flag to the parse()
function:

xml_schema::document doc_p (...);

doc_p.parse ("test.xml", xml_schema::flags::dont_initialize);


> 2. We are getting an error "Type XMLPlatformException, message: Could 
> not close the file", does xsd use a static or dynamic file handle to 
> reading the files? When and where is the file closed?

In this case the file opening and closing is performed by Xerces-C++ and
there appears to be a bug that is fixed for the next version of
Xerces-C++:

https://issues.apache.org/jira/browse/XERCESC-1658

This problem only occurs on Solaris, right?


> Would there be a way that I can call the file closer to close the file

> within say the post of the root tag?

If you don't use XML Schema validation in Xerces-C++ then you can handle
file opening/closing yourself and pass an std::istream to the parse()
function instead of a file name:

#include <fstream>

std::ifstream ifs ("test.xml");

if (!ifs.open ())
{
  // could not open
}

...

doc_p.parse (ifs, "test.xml", xml_schema::flags::dont_initialize);

...

ifs.close ();

If you do use XML Schema validation, then this method may not work
because Xerces-C++ will still be opening the schema files. For this
situation I can extract the fix from Xerces-C++ SVN and send it to you.


hth,
-boris




More information about the xsd-users mailing list