hi monks, im new to perl n xml. im working on a xml where i need to replace some nodes. the xml is of the format...

<list> <module name="all"> <moduleref name="a"> <moduleref name="b"> <moduleref name="c"> </module> <module name="a"> details details </module> <module name="b"> details details </module> </list>

Here i want replace the moduleref "a" node in the series "all" with the module "a" node along with the deatils, so my output should be something like shown below, which is what im not able to get..

<list> <module name="all"> <module name="a"> details details </module> <module name="b"> details details </module> <moduleref name="c"> </module> </list>

the code is...

use XML::DOM; my $xml_file = “./sample.xml”; my $parser = new XML::DOM::Parser; my $doc = $parser->parsefile($xml_file); my $module_node = $doc->getElementsByTagName(“module”); my $module1 = $module_node->item(0); my $module1_node = $module1->getElementsByTagName(“moduleref”); for(my $i=0;$i<$module1_node->getLength;$i++){ my $node = $module1_node->item($i); my $node_name = $node->getAttribute(“name”); my $new_node = &getModule($node_name); if($new_node){ replace($node,$new_node); } else{ do nothing… } } sub getModule{ my $module_name = shift; for(my $i=1;$i<$module_node->getLength;$i++){ my $sub_node = $module_name->item($i); my $sub_node_name = $sub_node->getAtrribute(“name”); if($module_name eq $sub_node_name){ return $sub_node; } } }

using simply replace is not working.. im not sure where im going wrong...


In reply to replace node in xml by joeperl

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.