Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

XML-Twig paste

by zuma53 (Beadle)
on Oct 16, 2012 at 19:12 UTC ( #999402=perlquestion: print w/replies, xml ) Need Help??
zuma53 has asked for the wisdom of the Perl Monks concerning the following question:


I've been using $foo->paste('last_child', $bar) to graft subtrees onto a larger tree and it's been working as expected. That is, the paste will *copy* the contents of foo onto bar.

Now, it seems that the paste is using a pointer of foo to the bar tree. Since foo is populated within a loop with a "new" for foo at the start of the iteration, each time the new is hit, the contents of the prior foo (now attached to bar) vanishes. What persists on bar is only the value of foo.

Strangely, the population of the nodes within foo is done via the same paste/last-child mechanism, but this works as expected.

Can someone clarify this? Or am I doing something wrong? Thanks.

use XML::Twig; my $objectsTwig = new XML::Twig; $objectsTwig->parse('<objects />'); $OBJECTS_ROOT = $objectsTwig->root; do { my $object = new XML::Twig; $object->parse('<object />'); $OBJECT_ROOT = $object->root; ... Add elements to <object> node $OBJECT_ROOT->paste('last_child', $OBJECTS_ROOT); } until (yada...);

Replies are listed 'Best First'.
Re: XML-Twig paste
by Anonymous Monk on Oct 16, 2012 at 20:01 UTC
    Processing XMl efficiently with XML::Twig - Section 4 shows this that works
    #!/usr/bin/perl -- use strict; use warnings; use XML::Twig; my $objectsTwig = XML::Twig->new; $objectsTwig->xparse('<objects />'); print $objectsTwig ->toString, "\n"; my $OBJECT_ROOT = $objectsTwig->root; for( 1 .. 10 ){ XML::Twig::Elt->new( 'object' )->paste( 'last_child', $OBJECT_ROOT ); } print $objectsTwig ->toString, "\n"; __END__ <objects/> <objects><object/><object/><object/><object/><object/><object/><object +/><object/><object/><object/></objects>

    A root on the other hand seems doesn't like to be cut/paste while it is a root

    #!/usr/bin/perl -- use strict; use warnings; use XML::Twig; my $objectsTwig = XML::Twig->new; $objectsTwig->xparse('<objects />'); print $objectsTwig ->toString, "\n"; my $OBJECT_ROOT = $objectsTwig->root; for( 1 .. 10 ){ my $object = XML::Twig->new; $object->xparse('<object />'); my $orphan = $object->root; $object->set_root(undef); ### crucial $orphan->cut; $orphan->paste('last_child', $OBJECT_ROOT ); } print $objectsTwig ->toString, "\n"; __END__ <objects/> <objects><object/><object/><object/><object/><object/><object/><object +/><object/><object/><object/></objects>

    Bug? Intended behaviour? I don't know , but doesn't seem too important to me :)

      Yup, if you  warn  $root->cut; you'll get undef, naturally

      sub cut { my $elt= shift; my( $parent, $prev_sibling, $next_sibling, $last_elt); # you can't cut the root, sorry unless( $parent= $elt->{parent}) { return; }

      So whatever the reason is, its intended

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999402]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2018-07-17 21:41 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (378 votes). Check out past polls.