Dravidan has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, after going through the documentation about the module XML::LibXML::Document,XML::LibXML::Node, i have come up with the latest code,again the expected portion of xml elements from source is getting displaced by the variable $TargetContent but the same is not written to the target file $OutFileName. The Target file is created by the below mentioned way,I ending the write( using $writer->end() ) and closing the target file( using $OutXmlFile->close() ) before calling the function GetMappletList. I hope,this will give more idea about what i am trying to do

my $OutXmlFile = IO::File->new(">$OutFileName"); my $writer = XML::Writer->new(OUTPUT => $OutXmlFile,DATA_MODE=>1,DAT +A_INDENT=>5);
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"); $SourceRoot= $MpltXml->documentElement() +; my @SourceNode = $MpltXml->findnodes("// +MAPPLET"); my $SourceRootNode = $SourceNode[0]; $TargetXml->setDocumentElement( '/POWE +RMART/REPOSITORY/FOLDER' ); #$TargetXml->setDocumentElement( '/POW +ERMART/REPOSITORY/FOLDER' ); $TargetXml->adoptNode($SourceRootNode) +; #$TargetXml->adoptNode('/POWERMART/REP +OSITORY/FOLDER/MAPPLET'); my $TargetContent = $TargetXml->addChi +ld($SourceRootNode); print $TargetContent; } } close(GET_MAPPLET_LIST); }

Replies are listed 'Best First'.
Re: Copy portion of XML elements from one XML file to another exiting XML file
by Corion (Patriarch) on Jan 25, 2014 at 08:26 UTC

    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.

      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.