mvitor has asked for the wisdom of the Perl Monks concerning the following question:

I monks, i have a problem with XML::Twig module, im parsing and reading a xml file in this way:
use XML::Twig; my $twig = XML::Twig->new(keep_encoding => "true"); $twig->parse("$string"); foreach my $product ($twig->root->children('produto')) { my $sibling = $product->first_child(); my %produto; do { $produto{$sibling->tag()} = $sibling->child_text(); } while($sibling = $sibling->next_sibling()); $product{nome} =~ s/\s+//g; ...................... }
My xml structure is in this way:
<produtos> <produto> <codigo>I0000949</codigo> <nome>Acucareiro Acril 200g Cromus Acrylic Line</nome> <categoria>SALA DE JANTAR:ACESSÓRIOS PARA MESA</categoria> <marca>Fabrica da Pedra</marca> <preco></preco> <link>4,99</link> <imagem>http://imgproduto.casaevideo.com.br/jpg/ud-cozin/000094 +9b.jpg</imagem> <parcelamento> <parcelas>12</parcelas> <valor>0,53</valor> </parcelamento> </produto> ...... </produtos>
And my problem is:
How i can read the data from tags <parcelas> and <valor> is into the tag <parcelamento> ?
Anyone as $product{parcelamento}{parcelas}, but it it dont works.
Thanks,
mvitor

ps: sorry foy my english

Replies are listed 'Best First'.
Re: XML::Twig Question
by Jenda (Abbot) on Nov 01, 2007 at 19:15 UTC

    I believe what you are looking for is simplify():

    my $string = <<'*END*'; <produtos> <produto> <codigo>I0000949</codigo> <nome>Acucareiro Acril 200g Cromus Acrylic Line</nome> <categoria>SALA DE JANTAR:ACESSORIOS PARA MESA</categoria> <marca>Fabrica da Pedra</marca> <preco></preco> <link>4,99</link> <imagem>http://imgproduto.casaevideo.com.br/jpg/ud-cozin/000094 +9b.jpg</imagem> <parcelamento> <parcelas>12</parcelas> <valor>0,53</valor> </parcelamento> </produto> </produtos> *END* use XML::Twig; my $twig = XML::Twig->new(keep_encoding => "true"); $twig->parse( $string); foreach my $product ($twig->root->children('produto')) { my $data = $product->simplify(); # use Data::Dumper; # print Dumper($data); $data->{nome} =~ s/\s+//g; print "$data->{nome}, parselas: $data->{parcelamento}{parcelas}\n" +; # ...................... }
    And please do NOT use foo( "$variable"), the quotes are at best pointless. For strings they force Perl to make a copy, for numbers to convert to a string and for references they cause those to get stringified and become unusable. Just drop quotes around single variables untill you really know you do need them. Which is very very seldom.

    If you think you can't explain your problem well enough in English, do what you can and then explain it in your own language. I'm sure there are people here that can translate for you.

      thank you guys, the xml::simple is a soltion for the future,
      the method simplify for this moment. thanks ;)

      Márcio Vitor

Re: XML::Twig Question
by Anonymous Monk on Nov 01, 2007 at 15:44 UTC
    You might just want to try XML::Simple. It makes what your doing very.. simple :P And it looks like you'll need to make something recursive to navigate to children of children in your example. Of if you have a limited depth of XMLNS you could hardcode children of parcelamento.