use strict; use XML::Rules; my $parser = XML::Rules->new( style => 'filter', rules => { _default => 'raw', 'id,gpa' => 'raw extended', student => sub { my ($tag, $attrs, $parser) = @_[0,1,4]; my $id = $attrs->{':id'}{_content}; if (exists $parser->{parameters}{ $id }) { $attrs->{':gpa'}{_content} = $parser->{parameters}{ $i +d }; } return $tag => $attrs; } } ); $parser->filter( \*DATA, \*STDOUT, {'002' => 'B'}); __DATA__ <students> <student> <name>John</name> <id>001</id> <gpa>A</gpa> </student> <student> <name>John</name> <id>002</id> <gpa>C</gpa> </student> </students>
As with XML::Twig only the data of one student are kept in memory so this works even for huge files. If you needed to change the grades of several students, you can do it in one pass ... you just specify all the IDs and marks in the ->filter() call.
Jenda
Enoch was right!
Enjoy the last years of Rome.
In reply to Re: How can I replace a line (tag) in an XML file?
by Jenda
in thread How can I replace a line (tag) in an XML file?
by perlPractioner
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |