Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Parsing an XML file

by Jenda (Abbot)
on Aug 27, 2012 at 17:18 UTC ( #990025=note: print w/replies, xml ) Need Help??

in reply to Parsing an XML file

use strict; use XML::Rules; my $parser = XML::Rules->new( stripspaces => 7, rules => { substrate => sub { 'substrate' => $_[1]->{id}}, product => sub { '@products' => $_[1]->{id}}, reaction => sub { my %reactions; foreach (split / /, $_[1]->{name}) { $reactions{$_} = { substrate => $_[1]->{substrate}, pr +oducts => $_[1]->{products}}; } return '%reactions' => \%reactions; }, graphics => '', entry => sub { my @reactions = split ' ', (delete $_[1]->{reaction}); $_[1]->{reactions} = \@reactions if @reactions; return '%entries' => {$_[1]->{id} => $_[1]} }, pathway => 'pass' }); use Data::Dumper; print Dumper($parser->parsefile('d:\temp\pon01100.xml'));

Here's an example of how you could tweak the data structure as it's being read. It gives you a hash containing the name, org, title, image and link from the root tag and a hash of entries and a hash of reactions. The reactions contain only the stuff you said you need, the entries contain the data from the <entry> tag with the list of reactions split and converted into an array.

Enoch was right!
Enjoy the last years of Rome.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990025]
[choroba]: One of our latest pictures (2003)
[Discipulus]: ah aha ah..

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2018-06-18 09:36 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.