in reply to Re^4: Copy portion of XML elements from one XML file to another exiting XML file
in thread Copy portion of XML elements from one XML file to another exiting XML file

Maybe now is the time to familiarize yourself with XML::LibXML and the DOM?

For example, you might want to read XML::LibXML::Document for ->setDocumentElement, and XML::LibXML::Node for ->appendChild?

Replies are listed 'Best First'.
Re^6: Copy portion of XML elements from one XML file to another exiting XML file
by Dravidan (Novice) on Jan 25, 2014 at 23:02 UTC
    <pi> I appreciate if you can find out what is wrong with this updated code and give me the solution, as i started learning this XML module just a day before,i could not get handle of everything.

    sub GetMappletList { my $Target_node; my ($TableNm,$ColumnNm,$DomainNm,$AlgoNm); my @MaskingAlgorithmList; my @MappletNode; my $MpltRefFile_Dir = shift; my $MappletXmlName; open GET_MAPPLET_LIST, "<","$Profiler_Dir/$MpltRefFile_Dir/Inve +ntory.rpt" or die "ERROR : Couldn't Open $Profiler_Dir/$MpltRefFile_D +ir/Inventory.rpt $!"; while (my $InvtoryCol = <GET_MAPPLET_LIST> ) { chomp $InvtoryCol; ($TableNm,$ColumnNm,$DomainNm,$AlgoNm) = split(/ +,/,$InvtoryCol); if ($TableNm =~ /$SourceTableName/) { print " The Workflow xml nme is $OutFile +Name\n"; $MappletXmlName = "$Mapplet_Dir/$AlgoNm. +XML"; print "The Dir name is $MappletXmlName\n +"; my $MpltParser = XML::LibXML->new(); my $TargetXml = $MpltParser->parse_file( +"$OutFileName"); my $MpltXml = $MpltParser->parse_file("$ +MappletXmlName"); my @MappletNode = $MpltXml->findnodes( +'//MAPPLET'); my @TargetNode = $TargetXml->findnodes +('//FOLDER'); #my $TargetRootNode = $TargetXml->find +nodes('/POWERMART'); my $TargetRootNode = $TargetXml->get +DocumentElement; #$TargetXml->setDocumentElement( '/POWER +MART/REPOSITORY/FOLDER' ); for my $TgtNode (@TargetNode) { for my $node (@MappletNode) { $TgtNode->addChild( $no +de ); $TgtNode->setOwnerDocum +ent( $TargetXml ); #$MpltXml->toFile($OutF +ileName); print "The Value is $Tg +tNode"; } } } } close(GET_MAPPLET_LIST); }

      Sorry, but teaching how the DOM works and how to apply the methods is far beyond what I can do for you.

      Your proposed code seems to confuse the source and destination. At least to me it appears that you are taking the elements of the target document and the elements you want to copy to the target document. ->setOwnerDocument needs to be called on nodes that have been removed from another document. Most likely, this would be called on nodes from your source document and not nodes in your target document.