Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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.

Jenda
Enoch was right!
Enjoy the last years of Rome.


Comment on Re: Parsing an XML file
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990025]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-11-26 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (160 votes), past polls