0

I am new to Perl, I have a task to replace or remove comment part (<!--) in multiple XML files, after replace I need to move those multiple XML files to other directories. I am illustrating below my D:\folder1\ having many xml files a.xml, b.xml, c.xml so on, after replace I need to move all the files from folder1 to D:\folder2\.

I tried one file by replacing - and @@@ but I don't have idea to remove comment line (<!--add aaa -->) from xml files in directory.

My Perl code follow below

#!/usr/bin/perl

use strict;
use warnings;

my $tag = 'SHORT_DESC';

open my $input_file, '<', 'test.xml' or die $!;
open my $output_file, '>', 'test_out.xml' or die $!;

my $input;
{
  local $/;               #Set record separator to undefined.
  $input = <$input_file>; #This allows the whole input file to be read at once.
}

$input =~ s/&/@@@/g;
$input =~ s/^- (?=<)//gm;
$input =~ s/<header[^>]*>\K\s*<header[^>]*>//gis;
close $input_file or die $!;
print {$output_file} $input;
close $output_file or die $!;

And my XML is

<?xml version = '1.0' encoding = 'UTF-8'?>
<!-- Order details-->
<order>
<Names>
<!-- Names-->
<content>This is dummy content</content>
</Names>
</order>
4
  • Are you typing XML comments incorrectly for a specific reason? Why the reference to @@@? Also, my $input = do { local $/; <$input_file> }; . Also, you are not reading using UTF-8 layer. Commented May 20, 2015 at 15:43
  • Files coming like this I am not typing Commented May 20, 2015 at 17:02
  • Are you claiming comments in the files literally use <\!--? In that case, they are not XML. Commented May 20, 2015 at 23:11
  • Sinan my requirement is to remove that comment line from the file, its purely xml file, example purpose I changed the code. Commented May 21, 2015 at 1:14

1 Answer 1

3

Use a proper XML handling module. I like XML::XSH2, a wrapper around XML::LibXML:

#! /usr/bin/perl
use warnings;
use strict;

use XML::XSH2;   # To handle XML.
use Path::Tiny;  # To move files around.

my ($source, $target) = @ARGV;

for my $file (path($source)->children(qr/\.xml$/)) {
    xsh "open $file ; delete //comment() ; save";
    path($file)->copy($target);
    path($file)->remove;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, could you guide me where I to include the source and destination folder path, how to include xsh2 module to the program, it is possible other method can implement my task.
@NachiappanR: The source and target directories should be specified as parameters to the script.
Thanks choroba, can you say did I need to include xsh2 module, if so please tell me the procedure.
Even I install the xsh2 using ppm install xsh2 in command prompt but I am facing the error Can't locate XML/XSH2.pm in @INC (you may need to install the XML::XSH2 module) (@INC contains: C:/Perl/site/lib C:/Perl/lib .) at searcn.pl line 5. BEGIN failed--compilation aborted at hello.pl line 5.
I hope the code will work, but only problem is how to include the module XSH2, is any other method to include the same task?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.