Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

XML::Twig inserting a new element, not first or last

by slugger415 (Beadle)
on Mar 04, 2013 at 17:16 UTC ( #1021679=perlquestion: print w/ replies, xml ) Need Help??
slugger415 has asked for the wisdom of the Perl Monks concerning the following question:

Hello esteemed monks, I'm learning XML::Twig and have not been able to figure out how to insert an element in a particular location other that first_child or last_child. For example, here is my XML source:

<concept> <title>My Title</title> <prolog>some other info</prolog> </concept>

I want to insert a new element "short_desc" in between "title" and "prolog". How do I do that?

I can do this using first_child and last_child thusly:

my($twig,$con) = @_; $con->insert_new_elt('first_child','short_desc' =>{ AAA => 'hello'});

which results in

<short_desc AAA="hello"/> <title>Bat sonar</title> <prolog>some other info</prolog>

but what I want is:

<title>Bat sonar</title> <short_desc AAA="hello"/> <prolog>some other info</prolog>

Any pointers appreciated. I have been scanning the Twig doc but am not sure what to look for. I see mention of $opt_position but this is not defined anywhere. (The xml twig site seems to have been dead for a while...)

Thank you -- Scott

Comment on XML::Twig inserting a new element, not first or last
Select or Download Code
Re: XML::Twig inserting a new element, not first or last
by choroba (Abbot) on Mar 04, 2013 at 17:22 UTC
    Scan for the "position options" and you will find before, after and within.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Wonderful, thank you!

Re: XML::Twig inserting a new element, not first or last
by mirod (Canon) on Mar 05, 2013 at 12:18 UTC
    The xml twig site seems to have been dead for a while...

    Are you sure? xmltwig.org should be working fine.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-12-25 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls