Re: Memory problem with XML::DOM::Parser???

From: Ben Morrow (usenet_at_morrow.me.uk)
Date: 06/06/04


Date: Sun, 6 Jun 2004 03:52:10 +0000 (UTC)


Quoth markus.mohr@mazimoi.de:
>
> Now, here is the code, and that's prety all I have to master.
>
> Do you think there is anything to do about rwriting this piece of code
> for XML::LibXML2?
>
> ------- Code sample -------
> #!/usr/bin/perl -w

<standard moan>
use strict;
use warnings;

> #------------------------------------------------------------------------------#
> # CFilter.pm
> #
> #
> #
> # Modul für die Filter-Funktionen des Client im Zusammenspiel mit
> CGUI.pm und #
> # CXML.pm
> #
> #------------------------------------------------------------------------------#

Big box comments like this really don't help readability; and info about
what the module is and does should be put in POD so it can be read later
more easily.

> use CXML;

What is this module? It's not on CPAN, so I presume it's yours? By the
looks of things this will need rewriting as well.

> # Pragmata
> use diagnostics;
> use strict;

Oh right, you've got it down here... use strict and warnings should come
first.

> use open ':utf8';

If you say

use open ':encoding(utf8)';

you will get better error handling and fallback facilities when the data
isn't valid.

> return 1;

Don't do this... put it at the end.

> sub import_anfrage ($$) {
> my ( $self, $anfrage, $konfiguration ) = @_;
> print "\nDie ANFRAGE wird imporiert:\n";
> print "---------------------------\n";
>
> open( TEMP, ">./anf_temp.anf" );
> print TEMP $anfrage;
> close TEMP;

You don't need to do this. XML::DOM and XML::LibXML can both parse XML
from a string (though I admit that in the case of XML::DOM the
documentation is less than clear...).

> # Wir legen ein neues XML-Objekt an, das alte wird verworfen
> my $xml = CXML->new();
> $xml->construct_xml($konfiguration);
> $xml = $konfiguration->get_value('xml');
> my $xml_root = $xml->{'root'};

Here is your first problem. CXML objects appear to contain XML::DOM
objects; AFAIK there is no way to transfer a node from an XML::LibXML
tree to an XML::DOM tree short of serialising it and re-parsing. This
means you will have to modify CXML to use XML::LibXML (or whatever) as
well.

> # Die Anfrage wird in ein XML-Dokument geparst
> print "Debug: -> Die ANFRAGE wird gePARSt.\n";

Debug messages like this are better sent to stderr with warn.

> unlink("./anf_temp.anf");

... or die translate_to_German("couldn't delete auf_temp.anf: $!");

> # Die Anfrage ist Teil der neuen EPA
> my $anfrage_root = $anfrage_doc->getElementsByTagName('ANFRAGE');
> $anfrage_root = $anfrage_root->item(0);
> $anfrage_root->setOwnerDocument( $xml->{'doc'} );
> my $nodes = $xml_root->getElementsByTagName('anfragen');
> my $node = $nodes->item(0);
> $node->appendChild($anfrage_root);

All of this stuff will be the same with XML::LibXML, once you have your
CXML object using the same DOM library.

In theory, as the DOM provides a specification of the methods etc., you
should simply be able to switch 'XML::LibXML' for 'XML::DOM' throughout
and it'll all be fine... it won't, of course (life's never that simple),
but the changes required shouldn't be major.

Ben

-- 
               We do not stop playing because we grow old; 
                  we grow old because we stop playing.
                            ben@morrow.me.uk


Relevant Pages

  • Re: Expanding tree paths
    ... > my %tree=( ... Your code is not strict compliant. ... Your code generates three warnings ... If that element has an entry in the hash, we call recurse() on each of ...
    (comp.lang.perl.misc)
  • Re: [PATCH 1/4] ide: add ide_dev_is_sata() helper (take 2)
    ... Sergei Shtylyov wrote: ... ide_dev_is_satahelper fixing it along the way to be more strict while ... This is still against the current Linus' tree and unfortunately I was able to ...
    (Linux-Kernel)
  • Re: XML::Parser Tree Style
    ... What did you see when you looked at the output of Dumper $tree? ... use strict; ... use warnings; ... print Dumper($tree); ...
    (comp.lang.perl.misc)
  • Re: problem creating string from list
    ... > I have been playing around with this for a while now and am clearly missing ... > use strict; ... > print $string; ...
    (comp.lang.perl.misc)
  • Re: Zidane = Jordan
    ... SMT wrote: ... or indifferent performances than great ones. ... strict. ... But I'm talking about how he is playing now, ...
    (rec.sport.soccer)