I want two CPAN modules, which may or may not exist:
XML::Filter::Simple
An XML::SAX handler that does what XML::Simple does. So if I have some XML like this:
<foo a=1>
<bar>baz</bar>
<bar>bez</bar>
</foo>
I would be able to do this:
my $foo = XML::Filter::Simple();
my $parser = XML::SAX::ParserFactory->parser( Handler => $foo );
$parser->parse_string( $xml );
## same kind of data structure as XML::Simple
print $foo->{ a }; # prints 1
print $foo->{ bar }[0]; # prints baz
print $foo->{ bar }[1]; # prints bar
Politics::US
I want to be able to keep up to date with the goings on of my congressman, senators, and I want Perl to help me.
use Politics::US::Senator;
use Politics::US::Bill;
my $senator = Politics::US::Senator->new( 'Durbin, Richard' );
foreach my $vote ( $senator->votes() ) {
my $decision = $vote->decision();
my $billNum = $vote->billNumber();
my $bill = US::Politics::Bill->new( $billNumber);
"Today Durbin cast a vote of $decision regarding $billNum\n",
"The bill's title is: ", $bill->title(), "\n"
"And here is the content of the bill:\n",
$bill->content();
}
Between the Senate website, and Thomas and WWW::Mechanize this isn't so far fetched at all.
No need for XML::Filter::Simple (Score:2, Informative)
XML::Simple can pretty much do that already. The difference is that in your example you ignore the return value from the parse and the hashref (or 'simple tree') ends up inside the handler object itself. But this is how you'd do it with XML::Simple:
Re:No need for XML::Filter::Simple (Score:3, Funny)
Re:No need for XML::Filter::Simple (Score:2)
Re:No need for XML::Filter::Simple (Score:2, Informative)
You might want to look into XML::Filter::Dispatcher which I think can do what you want (ie: treat sections of the document as documents in their own right).
Digging inside XML::Simple.pm wouldn't actually help (and might harm your sanity) since all the handler does is accumulate the events in an XML::Parser Tree-style structure and then once the whole document has been parsed it uses the 'collaspe' method to convert it to a simple tree.
Re:No need for XML::Filter::Simple (Score:2)
A little late... you can do this using XML::Twig: the latest version lets you use the simplify method on any element of the tree. simplify gives you the same structure as XML::Simple. And of course you can use it during the parsing, so you can deal with parts of the tree.
Example:
mirod
Re: Politics::US (Score:1)
------------------------------
You are what you think.
Re: Politics::US (Score:2)
Re: Politics::US (Score:1)
I'd definately be interested to hear about it -- I personally try to be a political agnostic, but it doesn't always work.
------------------------------
You are what you think.