in reply to parsing a simple xml response

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.

Replies are listed 'Best First'.
Re^2: parsing a simple xml response
by BrowserUk (Patriarch) on Jul 07, 2010 at 07:39 UTC
    Actually, looks like XML::Simple is not shorter.

    Of course it is. my $xml = XMLin( ... ); No one but you would bother to wrap it up in all that unnecessary verbiage.

    It's just slower and more fragile.

    I wonder. How much less time than 0.2 seconds (0.4 if we include the time taken to load perl and the libraries) does LibXML take?

    And given this is being read back from a website, will it make a Gnat's cock of difference?

    c:\test>timeit \perl64\bin\perl.exe junk4.pl Took 0.168277 seconds { ano => 22222222, cn => "1111222233334444555", custid => 101010101, description => "Transfer Completed", kf => {}, neta => "7.00", prid => "X111111111111111", responsecode => 11, status => "success", trxid => 33333333, } Took: 0.381164000 seconds

    After XML::Simple, there's only one other XML::* module worth considering. XML::Twig. Because it's the only one that will deal effectively with bigger than memory XML files.

    And before you respond, realise that any sentence that start with "If...", doesn't mean damn thing.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      No one but you would bother to wrap it up in all that unnecessary verbiage.

      PREFERRED_PARSER ensures XML::Parser is used. It's *by far* the fastest backend for XML::Simple.

      And given this is being read back from a website, will it make a Gnat's cock of difference?

      Seriously? You're complaining that I gave a faster alternative? Had it come of at the cost of code readability or extra work, that would have been worth pointing out.

      The difference is significant where I use it (and yes, we pull the response from the web).