On Fri, 2008-09-26 at 18:02 +0300, Vyacheslav Karamov wrote:
> Hi All!
> 
> I'm parsing XML using XML::Parser::Expat
> 
> ...
>     $self->{parser}->setHandlers(
>                             Start=>\&OnStartElement,
>                             End=>\&OnEndElement,
>                             Char=>\&OnCharacters);
> 
> ...
> sub OnStartElement
> {
>     my $parser = shift;
>     my $el     = shift;
>     %attrs     = shift if @_ > 2;

my %attrs = @_;
# The remaining parameters form a hash

> ...
> }
> 
> In %attrs parser stores XML tag attributes names and values.
> After some processing I need to store tags and attributes back to XML.
> But how could I preserve attributes order?
> 
> So, first I need to save attributes in array in order to save order?
> How can I do it?
> 

my @X = ();

sub OnStartElement {
  my $parser = shift @_;
  my $el = shift @_;
  my %attrs = @_;

  push @X, {
    el => $el,
    attrs => { %attrs },
    char => '',
  };
}

sub OnEndElement {
  my $parser = shift @_;
  my $el = shift @_;

  my %attrs = %{ $X[-1]{attrs} };
  my $text = $X[-1]{char};
  pop @X;

  # Process the element here

}

sub OnChar {
  my $parser = shift @_;
  my $data = shift @_;

  $X[-1]{char} .= $data;
}

-- 
Just my 0.00000002 million dollars worth,
  Shawn

Linux is obsolete.
-- Andrew Tanenbaum


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to