And I should also include the snippet of XML that I am parsing:#!/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 els +e 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 recursive +ness. # depth 1 elements will always have an id attribute. my $id_attribute = $element->{Attributes}{'{}id'}{Valu +e}; if ($config{debug}) { print "grabbing parent $id_attri +bute\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: \"$character +s->{Data}\"\n" }; if ($config{debug}) { print "encoded_chars: " . ::url_enco +de($characters->{Data}) . "\n" }; if ($depth >= 1) { if ($config{debug}) { print "pos[depth]: " . ${$pos[$d +epth]} . "\n" } # THIS IS THE PART I'M HAVING PROBLEMS WITH. I CAN'T G +ET 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->{N +ame}\"\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]);
<?xml version="1.0" standalone="yes"?> <library> <book id="book1011712400"> <title>Dreamcatcher</title> <author>Stephen King</author> <genre>Horror</genre> <pages>899</pages> <price> <currency>USA</currency> <amount>23.99</amount> </price> <rating>5</rating> </book> <book id="book1011712600"> <title>The Lord Of The Rings</title> <author>J. R. R. Tolkien</author> <genre>Fantasy</genre> <pages>3489</pages> <price> <currency>IT</currency> <amount>11.50</amount> </price> <rating>5</rating> </book> </library>
In reply to Setting the value of a complicated hash ref by blahblah
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |