in reply to Re: Printing element without nested children in XML::Twig
in thread Printing element without nested children in XML::Twig

Thanks Ouato,
I think I might have explained that a little badly, basically what I'm trying to do is add a button on a web page before each element and before each of its children, so basically I want to see the tree in series, with a button, then the first element, a button, then the first child of the first element, etc.
How would you go about removing and storing the nested children? Would that slow the program down a lot?
Also, I just found out that I'm getting this error about an element already being present in the tree, does that mean that an XML tree can't have duplicate elements, or that the IDs are the same?
Thanks for bearing with my rambling. :)
Jezzica85
  • Comment on Re^2: Printing element without nested children in XML::Twig

Replies are listed 'Best First'.
Re^3: Printing element without nested children in XML::Twig
by mirod (Canon) on Jul 26, 2007 at 15:23 UTC

    It seems to me that you are fighting the tool instead of using its strength. I think it would be easier to add the buttons directly in the twig, and then printing the entire twig at once. The loop would look like this (untested):

    foreach my $elt ($twig->root->descendants( '#ELT')) { $elt->insert_new_element( 'before', button => { attribute });} $twig->print;

    Of course the simple answer to your question is that the xml_text_only (I hate that name!) method would do it. But really, don't use it.

Re^3: Printing element without nested children in XML::Twig
by Ouato (Novice) on Jul 26, 2007 at 15:25 UTC
    Okay, I think I understand.
    I think the best solution is to add a "button" element as the first child of each element of the tree you want to display with a button.
    When printing, the first element to be displayed after your 'element' will be its button, and then the other childs.

    And yes, my 'solution' was bad and would have sensibly slow down the page.

    For your error, you are probably trying to add the same 'reference', meaning you don't create a new element before adding it, you only 'edit' the old one properties and try to add it again, but i'm not used to XML::Twig and I didn't see your code.