i want to find an element and i need to get the element with all attributes and store in to an variable. how can i do that. i am pasting below the xml and my code here. i need to store to $tagname.
2. How can i add new parent name to my "textbox" element? i need to add <qxp> as parent name. i should not rename the parent existing.

use strict; use warnings; use XML::Parser; use XML::Twig; my $xmlfile = 'input1.xml'; # the file to parse # initialize parser object and parse the string my $parser = XML::Parser->new( ErrorContext => 2 ); eval { $parser->parsefile( $xmlfile ); }; # report any error that stopped parsing, or announce success if( $@ ) { $@ =~ s/at \/.*?$//s; # remove module line number print STDERR "\nERROR in '$xmlfile':\n$@\n"; exit; } my $twig=XML::Twig->new(twig_handlers=>{ indd_document=>\&root_process, div => \&div_handler, content=> \&content_handler, }); $twig->parsefile( $xmlfile); open(FH,">output.xml") or die "cannot open output.xml: $!"; my $temp = $twig->toString; print FH $temp; close (FH); $twig->purge; #root tag "indd_document" process here sub root_process { my( $twig, $root)= @_; $root->set_tag( 'document'); $root->set_atts({OutputCreationDate=>'20090510', OutputCreationT +ime=>'00:00:00', 'xml:space'=>"preserve"}); $root->purge; } #paragraph element process goes here sub div_handler { my ($twig, $div)= @_; $div->set_tag('paragraph'); $div->del_att('cstyID','pstyID'); my $attval=$div->att('class'); $attval=~s/P_//; $div->set_att(style => $attval); $attval=~s/_//g; $attval="P".$attval; $div->set_att(class =>$attval); } #content tag handler sub content_handler { my ($twig, $content)= @_; my $tagname=$content->xpath(q{//box[@textflowid="rc_u50c6"]}); print $tagname; #For textbox element if($content->{'att'}->{'type'} eq "text") { $content->set_tag('textbox'); my $parent='qxpobject'; } }

In reply to xml::twig find a element with all attributes by Selvakumar

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.