I want to comment a block of code in xml. Note that the tags will be similar. In the below xml code, I want to block the listener block for com.pkg1.class2. How do i do it ?
Thanks in Advance
<listener>
<listener-class>com.pkg1.class1</listener-class>
</listener>
.........
<listener>
<listener-class>com.pkg1.class2</listener-class>
</listener>
............
Please post detail what kind of output you want?
RudiC
September 3, 2013, 4:01pm
3
Like this:
awk '/com.pkg1.class2/' RS="</listener>\n" ORS="</listener>\n" file
.........
<listener>
<listener-class>com.pkg1.class2</listener-class>
</listener>
?
I want the xml file with that block commented out or removed.
---------- Post updated at 05:21 AM ---------- Previous update was at 05:19 AM ----------
This didn't work. What I wanted is the same xml file with the block i am searching for, commented out.
Using Perl Tie::File
#!/usr/bin/perl -w
use strict;
use Tie::File;
my $i;
my @array;
my $file = "./test.xml";
tie @array, 'Tie::File', $file or die;
for($i=0;$i<$#array;$i++) {
if($array[$i] =~ /<listener-class>com\.pkg1\.class2<\/listener-class>/) {
$array[$i] = '<!'.$array[$i].'-->';
$array[$i-1] = '<!'.$array[$i-1].'-->';
$array[$i+1] = '<!'.$array[$i+1].'-->';
}
}
untie @array;