You can keep a stack of contexts. Loop through line by line. Every time you find a begin line, push the name of the tag onto the stack and print the tag. (Keep an indent counter as well so that you can make pretty XML code).
Every time you find an end line. print the close tag for the top of the stack then pop the stack.
Any other line just prints itself as a tag:
my @stack;
my $ind=0;
while (<LINES>) {
if (/^\s*Begin\s+(\w+)\s+(\w+)$/) {
print (' ' x $ind) . "<$1 name='$2'>\n";
push @stack,$1;
$ind += 3;
} elsif (/^\s*End\s*/) {
$ind -= 3;
print (' ' x $ind) . "<" . (pop @stack) . ">\n";
} else {
print (' ' x $ind) . "<$_>\n"; # change to print tag however y
+ou want
}
}
My machine is going haywire and won't cut and paste, or print, or open any new windows, so I can't really test this, but the algorithm is sound. You'll probably want to add error checking and such.
Good Luck,
-pete
Entropy is not what is used to be.
Added ".frm" to title 2002-02-14 dvergin