http://qs321.pair.com?node_id=583314


in reply to XML parsing Help..

The problem seems to be solved already ... but I liked playing around with it a bit. To ease this for fellow monks who might feel likewise - here is my code, showing similar behaviour to what bioswami wanted to achieve originally.
use strict; use warnings; use XML::Simple; use Data::Dumper; local $/; # slurp my $data = XMLin(<DATA>, KeyAttr => [], ForceArray => [qw/substrate pr +oduct/]); for my $reaction (@{$data->{reaction}}) { print $reaction->{name}, ' (', $reaction->{type}, "):\n"; for my $substrate (@{$reaction->{substrate}}) { foreach my $product (@{$reaction->{product}}) { print "\t", $substrate->{name}, " -- ", $product->{name}, +"\n"; } } } __DATA__ <root> <reaction name="rn:R00710" type="reversible"> <substrate name="cpd:C00084"/> <product name="cpd:C00033"/> </reaction> <reaction name="rn:R00014" type="irreversible"> <substrate name="cpd:C00068"/> <substrate name="cpd:C00022"/> <product name="cpd:C05125"/> </reaction> </root>

-- Hofmator

Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^2: XML parsing Help..
by Jenda (Abbot) on Nov 11, 2006 at 14:58 UTC

    Just in case anyone is interested, here are two implementations using XML::Rules. The first one prints the reactions as it parses through the XML, the other builds a more simplified data structure.

    use strict; use warnings; use XML::Rules; use Data::Dumper; my $parser = XML::Rules->new( rules => [ substrate => sub {return '@substrates' => $_[1]->{name}}, product => sub {return '@products' => $_[1]->{name}}, reaction => sub { print "$_[1]->{name} ($_[1]->{type}):\n"; print "\tSubstrates: ", join(", ", @{$_[1]->{substrates}}) +, "\n"; print "\tProducts: ", join(", ", @{$_[1]->{products}}), +"\n\n"; return; }, ], ); $parser->parse(\*DATA); __DATA__ <root> <reaction name="rn:R00710" type="reversible"> <substrate name="cpd:C00084"/> <product name="cpd:C00033"/> </reaction> <reaction name="rn:R00014" type="irreversible"> <substrate name="cpd:C00068"/> <substrate name="cpd:C00022"/> <product name="cpd:C05125"/> </reaction> </root>
    and
    use strict; use warnings; use XML::Rules; use Data::Dumper; my $parser = XML::Rules->new( rules => [ substrate => sub {return '@substrates' => $_[1]->{name}}, product => sub {return '@products' => $_[1]->{name}}, reaction => sub { my $name = delete($_[1]->{name}); delete($_[1]->{_content}); return $name => $_[1]; }, root => 'pass no content', ], ); my $result = $parser->parse(\*DATA); print Dumper($result); while (my ($reaction, $data) = each(%$result)) { print "$reaction ($data->{type})\n"; print "\tSubstrates: ", join(", ", @{$data->{substrates}}), "\n"; print "\tProducts: ", join(", ", @{$data->{products}}), "\n\n"; } __DATA__ <root> <reaction name="rn:R00710" type="reversible"> <substrate name="cpd:C00084"/> <product name="cpd:C00033"/> </reaction> <reaction name="rn:R00014" type="irreversible"> <substrate name="cpd:C00068"/> <substrate name="cpd:C00022"/> <product name="cpd:C05125"/> </reaction> </root>

    The substrate and product rules could be rewriten like this:

    'substrate,product' => sub {return '@'.$_[0].'s' => $_[1]->{name}},