in reply to Retrieving from a hash in Insertion Order

You may add a SAX Filter to XML::Simple that insert your order number for you on the fly.
Boris
  • Comment on Re: Retrieving from a hash in Insertion Order

Replies are listed 'Best First'.
Re^2: Retrieving from a hash in Insertion Order
by Scarborough (Hermit) on Jun 23, 2004 at 10:12 UTC
    Thank you for this advice, I'm not sure what a SAX filter is but I'll dig a little deeper.
    Thanks again
      Ok, here is a example, that add a attribute order to every element b.
      #!/usr/bin/perl use strict; use warnings; package MySAXHandler; use base qw(XML::SAX::Base); our $counter = 0; use Data::Dumper; sub start_element { my ( $self, $el ) = @_; if ( $el->{LocalName} eq 'b' ) { unless ( ( $el->{Attributes}->{'{}order'}->{Value} || 0 ) != 0 ) { $el->{Attributes}->{'{}order'} = { 'LocalName' => 'order', 'Prefix' => '', 'Value' => $counter++ +, 'Name' => 'order', 'NamespaceURI' => '' }, } } # process element start event $self->SUPER::start_element($el); } 1; use XML::Simple; use Data::Dumper; use XML::SAX; my $xml = <<"ENDE"; <opt> <a> <b a="12" attr_c="13">test</b> <b a="12" attr_c="14">test</b> <b a="12" attr_c="15">test</b> <b a="12" attr_c="16">test</b> <b a="12" attr_c="17">test</b> </a> </opt> ENDE my $simple = XML::Simple->new( ForceArray => 1); my $filter = MySAXHandler->new( Handler => $simple ); my $parser = XML::SAX::ParserFactory->parser( Handler => $filter ); my $ref = $parser->parse_string($xml); print Dumper($ref);
      Boris