You are viewing a plain text version of this content. The canonical link for it is here.
Posted to p-dev@xerces.apache.org by Bin Yan <bx...@health.state.ny.us> on 2003/12/18 19:49:02 UTC

cannot remove file after XercesDOMParser validation

Hi, All:

First mesg on this list.  Here is my problem. I have to user XML::Xerces
(2.3.0) to validate xml file against xsd. It works fine and generates
informative errors when validation failed. The problem is that I need to
remove the files after validation no matter if the validation passes or
fails. But with the following code, when falidation errors generated, the
XML file can never be unlinked. When validation is good, the file can be
removed. I think the file handler is never been released by the parser.
What am I missing here? Thanks.

Tony Yan

####

my $parser = XML::Xerces::XercesDOMParser->new();

eval {

$parser->setValidationScheme (1);
$parser->setDoNamespaces (1);
$parser->setCreateEntityReferenceNodes(1);
$parser->setDoSchema(1);
$parser->setExternalNoNamespaceSchemaLocation("sample.xsd");
$parser->setValidationSchemaFullChecking(1);


};
print $@ if $@;

my $error_handler = XML::Xerces::PerlErrorHandler->new();
$parser->setErrorHandler($error_handler);

###################### start to parse and/or validate #####################
my $xml_src;
eval {
  $xml_src = XML::Xerces::LocalFileInputSource->new("sample.xml");
  if ($xml_src) {
      $parser->parse ($xml_src);
  }

};
print $@ if $@;

############### clean up the files ############################
eval{
      $parser->reset();
      unlink("sample.xml") or print "cannot remove sample.xml";
      unlink("sample.xsd") or print "cannot remove sample.xsd";
};

print $@ if $@;






---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-p-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-p-dev-help@xml.apache.org


Re: cannot remove file after XercesDOMParser validation

Posted by "Jason E. Stewart" <ja...@openinformatics.com>.
"Bin Yan" <bx...@health.state.ny.us> writes:

> First mesg on this list.  

Welcome! I hope we can help you out.

> Here is my problem. I have to user XML::Xerces (2.3.0) to validate
> xml file against xsd. It works fine and generates informative errors
> when validation failed. 

Which platform are you using? I'm curious because this doesn't sound
like a Unix/Linux type issue.

> The problem is that I need to remove the files after validation no
> matter if the validation passes or fails. But with the following
> code, when falidation errors generated, the XML file can never be
> unlinked. When validation is good, the file can be removed. I think
> the file handler is never been released by the parser.  What am I
> missing here? 

Sounds very odd to me, but here would be what I would try:

1) scoping: place the parser in a different scope so that once parsing
   is complete, the interpreter is free to clean up the parser and all
   the memory/objects it contains.

  {
    my $parser = XML::Xerces::XercesDOMParser->new();

    # do parsing stuff
  }

  unlink('foo.xml');
  unlink('foo.xsd');

2) try putting the unlink code in an END block. This will allow perl
   to also clean up any resources that may be open and blocking the
   file removal.

  my $parser = XML::Xerces::XercesDOMParser->new();

  # do parsing stuff

  END {
    unlink('foo.xml');
    unlink('foo.xsd');
  }


  
> ####

[snip]

> eval{
>       $parser->reset();
>       unlink("sample.xml") or print "cannot remove sample.xml";
>       unlink("sample.xsd") or print "cannot remove sample.xsd";
> };

The code looks fine, and I would expect this code to work fine under
linux - what output are you getting?

Just FYI, the reset() call doesn't really do anything - it just sends
a message to any handlers you've registered that they should release
any data they stored as a result of the parse, but since the only
handler you've registered is a PerlErrorHandler it doesn't store any
parse info.

Cheers,
jas.

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-p-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-p-dev-help@xml.apache.org