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

    This should do:

    my $old_parent = $root; my $new_parent; my %modules; for my $node ($old_parent->childNodes()) { next if $node->nodeName() ne 'module'; my $mod_name = $node->getAttribute('name'); if ($mod_name eq 'all') { $new_parent = $node; } else { $old_parent->removeChild($node); $modules{$name} = $node; } } my %refcount; for my $node ($new_parent->childNodes()) { next if $node->nodeName() ne 'moduleref'; my $mod_name = $node->getAttribute('name'); my $referred_node = $modules{$name} or next; $referred_node = $referred_node->cloneNode(1) if $refcount{$mod_name}++; $new_parent->replaceChild($referred_node, $node); }

    A bit or error checking is needed. You didn't specify what you wanted to do with the modules that aren't referenced. The above removes them.

      hi ikegami,
      it worked, thanks a lot!!!
      but i had to change the function nodeName() to getNodeName() & childNodes() to getChildNodes()...
      probably some issue with perl versions i guess..
      in the actual file, there will be references for all the moduleref in "all", so no issues abt it..
      Thanks again...

        I was using XML::LibXML
Re: replace node in xml
by waldo (Scribe) on Dec 26, 2009 at 20:00 UTC

    Using XML::LibXML you can do (assuming you've put the XML in test.xml):

    #!/usr/bin/env perl use strict; use warnings; use XML::LibXML; my $doc = XML::LibXML->new()->parse_file('test.xml'); my $new_parent = ($doc->findnodes('//module [@name="all"]'))[0]; my @nodes = map {$_->getAttribute('name')} $doc->findnodes('//module [ +@name="all"]/moduleref'); my ($old_node, $new_node); foreach (@nodes) { $old_node = ($doc->findnodes('//moduleref [@name="' . $_ .'"]') )[ +0]; $new_node = ($doc->findnodes('//module [@name="'.$_ .'"]'))[0]; next unless $new_node; $old_node = $new_parent->replaceChild($new_node, $old_node); } print $doc->toString;

      hi waldo,
      thanks for your help, but im using XML::DOM, so what ikegami suggested works fine.
      couldnt run your code since LibXML is not installed in office..
      still thanks for your help..

Re: replace node in xml
by Anonymous Monk on Dec 26, 2009 at 16:36 UTC
    using simply replace is not working.. im not sure where im going wrong...

    First you need valid xml (mismatched tag at line 6, column 4,) and valid perl (Unrecognized character \x93, Unrecognized character \x85 )