joeperl has asked for the wisdom of the Perl Monks concerning the following question:
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...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: replace node in xml
by ikegami (Patriarch) on Dec 26, 2009 at 17:59 UTC | |
by joeperl (Acolyte) on Dec 27, 2009 at 08:19 UTC | |
by ikegami (Patriarch) on Dec 27, 2009 at 18:22 UTC | |
|
Re: replace node in xml
by waldo (Scribe) on Dec 26, 2009 at 20:00 UTC | |
by joeperl (Acolyte) on Dec 27, 2009 at 08:27 UTC | |
|
Re: replace node in xml
by Anonymous Monk on Dec 26, 2009 at 16:36 UTC |