Using XML::LibXML (Fast!):
use strict; use warnings; use XML::LibXML qw( ); my $xml = <<'__EOI__'; <?xml version = "1.0"?> <response> <custid>101010101</custid> <status>success</status> <responsecode>11</responsecode> <kf></kf> <cn>1111222233334444555</cn> <neta>7.00</neta> <prid>X111111111111111</prid> <ano>22222222</ano> <trxid>33333333</trxid> <description>Transfer Completed</description> </response> __EOI__ my $root = XML::LibXML->new->parse_string($xml)->documentElement; my %data = map { $_->nodeName => $_->textContent } $root->findnodes('/*/*');
Using XML::Simple (a little less code in this situation, but doesn't work if the elements can have attributes):
use strict; use warnings; use XML::Simple qw( :strict ); my $xml = <<'__EOI__'; <?xml version = "1.0"?> <response> <custid>101010101</custid> <status>success</status> <responsecode>11</responsecode> <kf></kf> <cn>1111222233334444555</cn> <neta>7.00</neta> <prid>X111111111111111</prid> <ano>22222222</ano> <trxid>33333333</trxid> <description>Transfer Completed</description> </response> __EOI__ $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; # Fastest backend my $parser = XML::Simple->new( ForceArray => [], KeyAttr => {} ); my $data = $parser->XMLin($xml);
Update: Actually, looks like XML::Simple is not shorter. It's just slower and more fragile.
In reply to Re: parsing a simple xml response
by ikegami
in thread parsing a simple xml response
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |