Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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}},


In reply to Re^2: XML parsing Help.. by Jenda
in thread XML parsing Help.. by bioswami

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found