I have the following two XML files.

books.xml
<Books> <Titles Mode="List"> <Title Name="Programming Perl"/> <Title Name="Advanced Perl Programming"/> <Title Name="Learning Perl on Win32 Systems"/> </Titles> </Books>
filter.xml
<Filters> <Titles> <Title Name="Programming Perl"/> <Title Name="Advanced Perl Programming"/> <Title Name="Learning Perl on Win32 Systems"/> </Titles> </Filters>
I am trying to do the following using the code below.
1. Change the 'Title Name' in 'books.xml' by appending the existing 'Title Name' with some random number.
2. Change the corresponding 'Title Name' in 'filter.xml'
use XML::Twig; # Learning XML testing functions ReadBooksInfo('books.xml','filter.xml'); sub ReadBooksInfo{ my $books_name = shift; my $filter_name = shift; my $filter_tree; my $books_tree= new XML::Twig( TwigHandlers => { Title => sub { my( $titletwig, $title_ele ) = @_; + my $titlename = $title_ele->att('Name'); my $unique_titlename = CreateUniqueFilterName($filterna +me); print "\nGenerated unique title name: ", $unique_title +name; print "\n"; $title_ele->set_att( Name => "$unique_titlename "); $filter_tree= new XML::Twig( TwigHandlers => { qq[Title[\@Name="$titlename"]] => sub { my ( $tree, $element ) = @_; $element->set_att( Name => "$unique_titlena +me" ); $element->print; } } ); $filter_tree->parsefile($filter_name); } } ); $filter_tree->parsefile( $books_name ); $filter_tree->print; $books_tree->print; } # Create unique filter names # =========================== sub CreateUniqueFilterName { my $user_filter_name = shift; my $lower=1000; my $upper=2000000; my $random = int(rand( $upper - $lower + 1 ) ) + $lower; my $unique_filter_name = $user_filter_name . "_". "$$". "_". "$^T" +. "_" .$random; }
Now the problem is I am able to correctly parse the items. But at at the end when I print the 'books_tree - twig' only the Title name 'Learning Perl on Win32 Systems' reflects the changes. Everything else is same as the original books.xml file.

Please tell me how to fix this?

In reply to Problem in parsing XML file using XMl::Twig by ramya2005

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.