in reply to Inserting new elements in an XML doc using XML::Twig

I don't know XML::Twig all that well, but the doc says:
XML::Twig::Elt

new ($optional_tag, $optional_atts, @optional_content)

The tag is optional (but then you can't have a content ), the $optional_atts argument is a refreference to a hash of attributes, the content can be just a string or a list of strings and element.

You are providing a list of arrayrefs where it's expecting strings or elements. Maybe do something like:
push @newElts, XML::Twig::Elt->new( keyword => {translate => 'true'}, +$_ ) for @keywords;

Replies are listed 'Best First'.
Re^2: Inserting new elements in an XML doc using XML::Twig
by GrandFather (Saint) on Feb 12, 2007 at 10:03 UTC

    Almost right. What I actually was after is:

    push @newElts, XML::Twig::Elt->new( keyword => $_ ) for @keywords;

    mumble, mumble, exactly right actually, mumble,mumble. Err, that is, yes you are exactly right. Thank you. :)

    which generates (when used in the original code):

    <?xml version="1.0" encoding="UTF-8"?> <topic> <keywords> <keyword translate="true">this</keyword> <keyword translate="true">that</keyword> <keyword translate="true">the</keyword> <keyword translate="true">other</keyword> </keywords> <body></body> </topic>

    I read that documentation forwards and backwards several times, looked at the samples following it, and just couldn't see what was wanted. Doh!

    Update: retract silly "almost right" statement. Forgot I needed the attribute!


    DWIM is Perl's answer to Gödel
      I like XML::Twig however the documentation leaves much to be desired. There are to many methods that do the same thing and the function call layout is not easy to follow.

        I agree with you on the docs. I am not quite sure what to do about it though.

        I think a first step would be to enhance the tutorial, by adding more examples and sections on more advanced topics. Maybe I should include it with the module too.

        Then... I don't know. Maybe check that the quickref card is complete and up to date.

        In any case there needs to be a reference, that documents each method. That's the current module doc. I would gladly accept any idea to improve it's layout, organisation... really anything about it.