Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Recursion and XML::Twig

by BioHazard (Pilgrim)
on Aug 25, 2005 at 10:20 UTC ( #486497=perlquestion: print w/ replies, xml ) Need Help??
BioHazard has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I just started with XML::Twig and I like it. But I need recursion during my parsing process. Whenever I call parse(...) or parsefile(...) inside a twig_handler everything works fine but the following line is printed out separately:

calling depth after parsing is finished at /usr/lib/perl5/vendor_perl/5.8.6/i686-linux/XML/Parser/Expat.pm line 474

Is there a way to avoid this message? Turning off -w did not do it. I see that it has got something to do with Expat but having taken a closer look to the specified .pm files did not help. Below there is a code snippet for better understanding:

#!/usr/bin/perl -w use strict; use XML::Twig; my $t = XML::Twig->new( twig_handlers => { child => \&onChild } ); $t->parse('<node id="17"><content>foo bar</content><child file="19.xml +" /></node>'); sub onChild { my ($t, $child) = @_; my $atts = $child->atts; $t->parsefile($atts->{file}); }


The file "19.xml" exists and its content does not matter. There is no further recursion in that file.
Thank you.

BioHazard
reading between the lines is my real pleasure

Comment on Recursion and XML::Twig
Select or Download Code
Re: Recursion and XML::Twig
by tlm (Prior) on Aug 25, 2005 at 11:10 UTC

    It's more than a message; it's a fatal error.

    Frankly, I would not expect the parser to brook such treatment. Why not create a new parser in onChild?:

    sub onChild { my ($t, $child) = @_; my $atts = $child->atts; XML::Twig->new( twig_handlers => { child => \&onChild } ) ->parsefile($atts->{file}); }

    the lowliest monk

      Oops!
      I did not recognize that it is a fatal error. Of course, the idea with a new twig object makes more sense and works perfectly.

      Thanks a lot :)

      BioHazard
      reading between the lines is my real pleasure
Re: Recursion and XML::Twig
by mirod (Canon) on Aug 25, 2005 at 13:36 UTC

    Darn, this is the second time I answer this question, and I can't find the previous answer, I guess it will have to go in the FAQ.

    Indeed you cannot re-use the twig object to parse an other document. Contrary to most other modules (XML::Parser, XML::LibXML...), the twig is both the parser _and_ the parsed document. You can re-use the object if you parse several documents sequentially, but you cannot re-use it within a parse. So in your case you have to create a new XML::Twig object.

    The reason for this is simple: incompetence. Mine. I wasn't very familiar with OO when I started writing the module, back in 1998, and I completely missed the object factory construct. Sorry.

Re: Recursion and XML::Twig
by mirod (Canon) on Aug 25, 2005 at 15:06 UTC

    OK, fixed I hope: FAQ entry added and better error diagnostic added in the development version of the module (tested as the last test in t/test_errors.t).

    Let me know if you have any comment.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://486497]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-10-23 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls