untitled

(ff) #1

258 10 Transforming with Traditional Programming Languages


use XML::Parser;
$p = new XML::Parser(Handlers =>
{ Start => \&start, End => \&end, Char => \&char });
$p->parsefile($ARGV[0]);
sub start {
$tag = $_[1];
if ($tag eq "HealthStudy") {
print("<HealthStudy>\n");
}
elsif ($tag eq "Interview") {
print("<Interview");
}
elsif ($tag eq "Date") {
print(" Date=’");
$printContent = 1;
}
...
}
sub char {
if ($printContent) {
print($_[1]);
}
}
sub end {
$tag = $_[1];
if ($tag eq "HealthStudy") {
print("</HealthStudy>\n");
}
elsif ($tag eq "Interview") {
print("/>\n");
}
elsif ($printContent) {
print("’");
$printContent = 0;
}
}

Program 10.26 Transforming XML content to XML attributes
Free download pdf