saintpretz59
01-15-2008, 07:32 AM
Hello everybody,
I'm making a script that should parse XML files (already done before, I know), and I'm having a bit of trouble. The sub accepts two strings: location of the xml file and the name of the element for the "main entries." (For a file of root <phonebook> with many individual entries <person>, you'd enter: person as an argument. The sub should then change that into </person>. The next step is to read off chunks of the XML file. Instead of reading of one line at a time, the $/ variable is set to </person>. After every occurrence of </person>, the sub should print of END OF ENTRY.
>>Not the most practical, obviously, but it should improve once i solve this problem:
Instead of printing END OF ENTRY at the appropriate places, it only prints it at the very end of the file.... Does anyone know why this would happen?
#!/usr/bin/perl
#form.cgi
use warnings;
use strict;
print "Content-type: text/plain\n\n";
sub xmlTag{
my $file = $_[0];
my $separator = "</" . "$_[1]" . ">";
$/ = "$separator";
print "We will be parsing $file with a separator of $/\.\n\n\n";
open(XMLFILE, "$file");
for (<XMLFILE>){
print "$_";
print "END OF ENTRY";
}
close(XMLFILE);
}
xmlTag("QuestDex.xml", "person");
I'm making a script that should parse XML files (already done before, I know), and I'm having a bit of trouble. The sub accepts two strings: location of the xml file and the name of the element for the "main entries." (For a file of root <phonebook> with many individual entries <person>, you'd enter: person as an argument. The sub should then change that into </person>. The next step is to read off chunks of the XML file. Instead of reading of one line at a time, the $/ variable is set to </person>. After every occurrence of </person>, the sub should print of END OF ENTRY.
>>Not the most practical, obviously, but it should improve once i solve this problem:
Instead of printing END OF ENTRY at the appropriate places, it only prints it at the very end of the file.... Does anyone know why this would happen?
#!/usr/bin/perl
#form.cgi
use warnings;
use strict;
print "Content-type: text/plain\n\n";
sub xmlTag{
my $file = $_[0];
my $separator = "</" . "$_[1]" . ">";
$/ = "$separator";
print "We will be parsing $file with a separator of $/\.\n\n\n";
open(XMLFILE, "$file");
for (<XMLFILE>){
print "$_";
print "END OF ENTRY";
}
close(XMLFILE);
}
xmlTag("QuestDex.xml", "person");