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

That may appear simple but I don't know why you would like to call your print loop before adding the things you want to add.

Why can't you simply add these things before calling your print loop ?

Nevermind... For you problem ,
A simple (bad?...) solution could be to 'remove and store' the nested childs of each node before calling your print, and restore them in the node after the print.
  • Comment on Re: Printing element without nested children in XML::Twig

Replies are listed 'Best First'.
Re^2: Printing element without nested children in XML::Twig
by jezzica85 (Initiate) on Jul 26, 2007 at 14:20 UTC
    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

      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.

      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.