Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: XML-Twig paste

by Anonymous Monk
on Oct 16, 2012 at 20:01 UTC ( #999415=note: print w/ replies, xml ) Need Help??


in reply to XML-Twig paste

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 :)


Comment on Re: XML-Twig paste
Select or Download Code
Replies are listed 'Best First'.
Re^2: XML-Twig paste
by Anonymous Monk on Oct 16, 2012 at 20:05 UTC

    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (15)
As of 2015-07-28 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls