Hello Monks,
I'm trying to get XML::SAX off the ground, but I'm not doing so well.
I'm doing:
#!/usr/bin/perl -Tw
use strict;
use CGI::Carp qw(fatalsToBrowser);
use XML::SAX::ParserFactory; # dynamically load an available parser, o
+r PurePerl if nothing else
my $userid = "alex";
my $dataset = "hello2.xml";
my $file = "/usr/home/$userid/$dataset";
my $handler = SAXHandler->new();
my $parser = XML::SAX::ParserFactory->parser( Handler => $handler);
$parser->parse_uri($file);
package SAXHandler;
sub new {
my $type = shift;
return bless {}, $type;
}
sub start_document {
my ($self, $element) = @_;
print "Starting document...\n";
}
sub start_element {
my ($self, $element) = @_;
print "Starting element $element->{Name}\n";
}
sub end_element {
my ($self, $element) = @_;
print "Ending element $element->{Name}\n";
}
sub characters {
my ($self, $characters) = @_;
print "characters: $characters->{Data}\n";
}
1;
With hello2.xml looking like:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE greeting [
<!ELEMENT greeting (#PCDATA)>
]>
<greeting>Hello, world!</greeting>
This is outputting the error:
Name contains invalid start character: '<'
Now, I know that that is UTF8 for the "<" character. But, XML needs to start with this character. I thought mabye it was the way I was calling parse_uri, so I tried opening a file handle and using parse_file. I tried pulling the whole file into a string and doing parse_string. I'm stumped.
Thanks,
Alex
P.S.- Are there any other pure-perl XML parsers out there? XML::SAX loads a billion modules and I just want something simple that doesn't require expat C compilation. Everything seems to rely on expat. I need portability.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.