in reply to Copy portion of XML elements from one XML file to another exiting XML file

Your code does not even compile. You can help us help you better by posting the actual code that you are running.

Can't declare constant item in "my" at tmp.pl line 1, near ");" No such class SourceXmlName at tmp.pl line 5, near "my SourceXmlName" syntax error at tmp.pl line 5, near "my SourceXmlName;" Execution of tmp.pl aborted due to compilation errors.

From looking through your source code, you seem to be very confused about how Perl declares variables. Maybe an introductory text about Perl helps, or perlsyn? Perl variables generally have a dollar sign in front of them.

Looking beyond all that, my guess is that sample.xml (or whatever file is named in the actuall error message which you don't show) does either not exist, or is empty, or does not contain valid XML.

Replies are listed 'Best First'.
Re^2: Copy portion of XML elements from one XML file to another exiting XML file
by Dravidan (Novice) on Jan 25, 2014 at 13:56 UTC

    Hi Corion - Sorry for confusing you, Here is my complete function GetMappletList() i am trying to call,but i am calling it from same script in which the function was declared, The variable $SourceTableName used in the if condition is using values from global variable declared in the script.I am trying to copy portion of xml elements from the source file $MappletXmlName to another file $OutFileName which too have some elements.

    sub GetMappletList { 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/) { $MappletXmlName = "$Mapplet_Dir/$AlgoNm. +XML"; my $MpltParser = XML::LibXML->new(); my $MpltXml = $MpltParser->parse_file("$ +MappletXmlName"); my $TargetXml = $MpltParser->parse_file( +"$OutFileName"); my @MappletNode = $MpltXml->findnodes('/ +POWERMART/REPOSITORY/FOLDER/MAPPLET'); for my $node (@MappletNode) { $TargetXml->appendChild +($node); #print $node->toString( +2); } } } close(GET_MAPPLET_LIST); }

      Again, what steps have you done to ensure that the file you give to ->parse_file exists and contains valid XML?

      Maybe it helps to print out $MappletXmlName and $OutFileName. Also, $AlgoNm seems to play a part in constructing the filename.

        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?