#!/usr/bin/perl -w # use strict; use diagnostics; $| = 1; my (%data, %config, @pos, $depth, $approved, $id_arrayref); $config{debug} = 0; # activate debug output get_ids_data(["book1011712600","book1011712400"]); sub get_ids_data { $id_arrayref = shift; if ($config{debug}) { foreach ( @{$id_arrayref} ) { print "requesting item: $_ \n"; } } my $file = "books_test.xml"; # dynamically load an available parser, or PurePerl if nothing else require XML::SAX::ParserFactory; import XML::SAX::ParserFactory; my $handler = MySAXHandler->new(); my $parser = XML::SAX::ParserFactory->parser( Handler => $handler ); open(FILE, $file); $parser->parse_file(\*FILE); close(FILE); package MySAXHandler; sub new { my $type = shift; return bless {}, $type; } sub start_document { my ($self, $element) = @_; print "Starting document...\n"; $depth = -1; # Omit the root element } sub start_element { my ($self, $element) = @_; $depth++; if ($config{debug}) { print "starting element \"$element->{Name}\"\n" }; if ($config{debug}) { print "depth: $depth\n\n" }; if ($depth == 1) { # At this point I would add a test to see if the ID # matched what we were looking for, but right now # I just want to grab everything to test the recursiveness. # depth 1 elements will always have an id attribute. my $id_attribute = $element->{Attributes}{'{}id'}{Value}; if ($config{debug}) { print "grabbing parent $id_attribute\n" }; $pos[$depth] = \$data{$id_attribute}; } elsif ($depth > 1) { $pos[$depth] = \${$pos[$depth - 1]}->{$element->{Name}}; if ($config{debug}) { print "child \"$element->{Name}\" is now parent\n" }; } } sub characters { my ($self, $characters) = @_; if ($config{debug}) { print "unencoded_chars: \"$characters->{Data}\"\n" }; if ($config{debug}) { print "encoded_chars: " . ::url_encode($characters->{Data}) . "\n" }; if ($depth >= 1) { if ($config{debug}) { print "pos[depth]: " . ${$pos[$depth]} . "\n" } # THIS IS THE PART I'M HAVING PROBLEMS WITH. I CAN'T GET THE CHARACTERS # ASSIGNED TO BE THE VALUE OF THE REFERENCED HASH KEY PROPERLY. # I HAVE COMMENTED IT OUT SINCE IT KILLS THE SCRIPT. # $pos[$depth] = main::url_encode($characters->{Data}); } } sub end_element { my ($self, $element) = @_; if ($config{debug}) { print "Ending element \"$element->{Name}\"\n" }; $depth--; } 1; # end of MySAXHandler package } # url-encode/decode routines lifted from CGI::Simple sub url_decode { my ( $decode ) = @_; return () unless defined $decode; $decode =~ tr/+/ /; $decode =~ s/%([a-fA-F0-9]{2})/ pack "C", hex $1 /eg; return $decode; } sub url_encode { my ( $encode ) = @_; return () unless defined $encode; $encode =~ s/([^A-Za-z0-9\-_.!~*'() ])/ uc sprintf "%%%02x",ord $1 /eg; $encode =~ tr/ /+/; return $encode; } require Data::Dumper; print "DUMPING DATA:\n"; print Data::Dumper->Dump([\%data, \@pos]); #### Dreamcatcher Stephen King Horror 899 USA 23.99 5 The Lord Of The Rings J. R. R. Tolkien Fantasy 3489 IT 11.50 5