http://www.perlmonks.org?node_id=928486


in reply to How can I replace a line (tag) in an XML file?

One can also do it without any XML parser. Here's an example

my $str = <<EOF; <students> <student> <name>John</name> <id>001</id> <gpa>A</gpa> </student> <student> <name>John</name> <id>002</id> <gpa>C</gpa> </student> </students> EOF $str =~ s/<student>(.*?)<\/student>/check_record($1)/gse; print $str; sub check_record { my $rec = shift; $rec =~ s/(<gpa>\s*)C(\s*<\/gpa>)/$1B$2/ if($rec =~ /<id>\s*002\s* +<\/id>/); return "<student>$rec</student>"; }