D
DJ Stunks
hey all,
I'm using XML:arser to parse an xml file which is not well-formed.
The source I receive it from formats it as:
$ cat data.xml
<row><data foo="a"/></row>
<row><data baz="b"/></row>
Obviously the parser chokes on this. If I manually add document tags
as follows, my script is fine:
$ cat fixed-data.xml
<d>
<row><data foo="a"/></row>
<row><data baz="b"/></row>
</d>
Question: script is below. What is the easiest way to add the
document tags such that the parser doesn't choke without the
additional step of manually adding them? I can pass a filehandle to
the parser if there was a way to add document tags to the filehandle,
but the file is very large and I couldn't think of an easy way to add
the data to the filehandle without slurping into a scalar.
TIA,
-jp
$ cat tmp.pl
#!/usr/bin/perl
use strict;
use warnings;
use XML:arser;
my $parser = XML:arser->new(Handlers => { Start =>
\&handle_start });
$parser->parsefile('fixed-data.xml');
sub handle_start {
my ($p, $el, %atts) = @_;
if ($el eq 'data') {
for my $k (keys %atts) {
print "$k: $atts{$k}\n";
}
}
}
__END__
I'm using XML:arser to parse an xml file which is not well-formed.
The source I receive it from formats it as:
$ cat data.xml
<row><data foo="a"/></row>
<row><data baz="b"/></row>
Obviously the parser chokes on this. If I manually add document tags
as follows, my script is fine:
$ cat fixed-data.xml
<d>
<row><data foo="a"/></row>
<row><data baz="b"/></row>
</d>
Question: script is below. What is the easiest way to add the
document tags such that the parser doesn't choke without the
additional step of manually adding them? I can pass a filehandle to
the parser if there was a way to add document tags to the filehandle,
but the file is very large and I couldn't think of an easy way to add
the data to the filehandle without slurping into a scalar.
TIA,
-jp
$ cat tmp.pl
#!/usr/bin/perl
use strict;
use warnings;
use XML:arser;
my $parser = XML:arser->new(Handlers => { Start =>
\&handle_start });
$parser->parsefile('fixed-data.xml');
sub handle_start {
my ($p, $el, %atts) = @_;
if ($el eq 'data') {
for my $k (keys %atts) {
print "$k: $atts{$k}\n";
}
}
}
__END__