in reply to Re^3: 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

Here is my updated code, i am able to print the exact node and associated sub elements i want to copy but how can i write the content of $node which is also getting printed in the output to the target file

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'); for my $node (@MappletNode) { print $node->toString(1 +); } } } close(GET_MAPPLET_LIST); }

The source file content is given below

<?xml version="1.0" encoding="Windows-1252"?> <!DOCTYPE POWERMART SYSTEM "powrmart.dtd"> <POWERMART CREATION_DATE="01/24/2014 18:27:23" REPOSITORY_VERSION="181 +.90"> <REPOSITORY NAME="xyz" VERSION="181" CODEPAGE="MS1252" DATABASETYPE="O +racle"> <FOLDER NAME="DM_POC" GROUP="" OWNER="Administrator" SHARED="NOTSHARED +" DESCRIPTION="" PERMISSIONS="rwx---rwx" UUID="c08574eb-cdf5-4bd1-bda +0-d08519348c79"> <MAPPLET DESCRIPTION ="" ISVALID ="YES" NAME ="mplt_Key_Lkp_NID" O +BJECTVERSION ="1" VERSIONNUMBER ="1"> *****----THERE ARE COUPLE of OTHER SUB ELEMENTS WHICH ARE NOT GIVEN HE +RE------**** </MAPPLET> </FOLDER> </REPOSITORY> </POWERMART>

The Target xml file content looks like the below one

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE POWERMART SYSTEM "powrmart.dtd"> <POWERMART CREATION_DATE="1/25/2014 14:39:55" REPOSITORY_VERSION="181. +90"> <REPOSITORY NAME="rep_brcls_punapp830" VERSION="181" CODEPAGE="MS +1252" DATABASETYPE="Oracle"> <FOLDER NAME="DM_POC" GROUP="" OWNER="Administrator" SHARED= +"NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx---rwx" UUID="c08574eb-cdf +5-4bd1-bda0-d08519348c79"> <SOURCE BUSINESSNAME="" DATABASETYPE="Flat File" DBDNAM +E="Flat_File" DESCRIPTION="" NAME="sample" OBJECTVERSION="1" OWNERNAM +E="" VERSIONNUMBER="1"> <FLATFILE CODEPAGE="US-ASCII" CONSECDELIMITERSASON +E="NO" DELIMITED="YES" DELIMITERS="," ESCAPE_CHARACTER="" KEEPESCAPEC +HAR="NO" LINESEQUENTIAL="NO" MULTIDELIMITERSASAND="NO" NULLCHARTYPE=" +ASCII" NULL_CHARACTER="*" PADBYTES="1" QUOTE_CHARACTER="NONE" REPEATA +BLE="NO" ROWDELIMITER="10" SHIFTSENSITIVEDATA="NO" SKIPROWS="0" STRIP +TRAILINGBLANKS="NO" /> ******-----SOMETHING HERE ,THEY ARE NOT GIVEN AS THEY ARE TOO BIG----- +------***** </SOURCE> </FOLDER> </REPOSITORY> </POWERMART>

I want to copy everything between the tags <MAPPLET> </MAPPLET> from source xml to target xml file and the copied content should be placed in the target, between <FOLDER> </FOLDER> tags

I am totally lost myself in finding correct solution to make this work, Please advise me how can i fix it?

Replies are listed 'Best First'.
Re^5: Copy portion of XML elements from one XML file to another exiting XML file
by Corion (Patriarch) on Jan 25, 2014 at 20:01 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.