Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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
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 chilling in the Monastery: (4)
As of 2014-09-21 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (166 votes), past polls