in reply to Re: Building simple tree from XML
in thread Building simple tree from XML

This way I can to decrease memory usage while processing a big XML files.

Replies are listed 'Best First'.
Re^3: Building simple tree from XML
by runrig (Abbot) on Oct 12, 2005 at 21:20 UTC
    ...decrease memory usage while processing a big XML files.
    That is what XML::Twig does handily.
Re^3: Building simple tree from XML
by mirod (Canon) on Oct 12, 2005 at 21:48 UTC

    But you seem to be building a tree in memory that would then be quite big too. So either I am missing something, or you gave us too little information to really help you.

      Thanks for you attention! You are right, I gave you too little information to help solving my problem. But at that time my handlers were absolutely "crude" and could not give the helpful information. I solved this problem. My handlers are below.
      sub start_document { my $self = shift; $self->{lists} = []; $self->{curlist} = $self->{tree} = []; } # # sub start_element { my $self = shift; my $element = shift; my $name = $element->{Name}; $self->{elem_name} = $element->{Name}; my $id; my $var; if ($element->{Attributes}) { $id = $element->{Attributes}->{'{}id'}->{Value}; $var = $element->{Attributes}->{'{}var'}->{Value}; } if ($name eq 'part') { my $newlist = [ {id=>$id, var=>$var} ]; push @{ $self->{lists} }, $self->{curlist}; if ($is_parent) { push @{$self->{curlist}[0]{childs}}, @{$newlist}; } else { push @{ $self->{curlist} }, @{$newlist}; } $self->{curlist} = $newlist; } $is_parent++ if $element->{Name} eq 'part'; } # # sub end_element { my $self = shift; my $element = shift; my $name = $element->{Name}; # Save template name; if ($name eq 'template') { $self->{template} = $self->{char}; } if ($name eq 'part') { $self->{curlist} = pop @{ $self->{lists} }; $is_parent--; } } # # sub characters { my $self = shift; my $chars = shift; my $text = $chars->{Data}; my $clist = $self->{curlist}; $self->{char} = codes2chars($text); } # # sub end_document { my $self = shift; delete ($self->{curlist}); delete ($self->{lists}); my $tree = $self->{tree}; delete ($self->{tree}); $struct->{$self->{menu_name}}->{parts} = $tree; $struct->{$self->{menu_name}}->{template} = $self->{template}; }
Re^3: Building simple tree from XML
by Skeeve (Parson) on Oct 12, 2005 at 21:20 UTC
    Try XML::Twig. It's very easy to use and you can throw away parsed data as soon as you are done with it.

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e