1

All, I have read manyother other posts and have not been able to quite get a handle on this. I am pulling data form a web service and i am returned the following XML:

$VAR1 = {
    'error'           => 'EndOfResults',
    'model-responses' => {
        'model' => [
            {   
                'attribute' => {
                    'content' => 'wltvbswfc02',
                    'id'      => '0x1006e'
                },
                'mh' => '0x100540'
            },
            {   
                'attribute' => {
                    'content' => 'wltvsutm1ds02',
                    'id'      => '0x1006e'
                },
                'mh' => '0x100c80'
            },
            {   
                'attribute' => {
                    'content' => 'wltvsdora03',
                    'id'      => '0x1006e'
                },
                'mh' => '0x100c49'
            },
            ]

    },
    'throttle'     => '86',
    'total-models' => '86',
    'xmlns'        => 'http://www.ca.com/spectrum/restful/schema/response'
};

I need to pull out 'mh' and 'content' and assign to a hash with content as key and mh as value. I have not been able to get data structure quite right.. I appreciate any help. Thanks! Robert

Miller
  • 34,962
  • 4
  • 39
  • 60
rj reilly
  • 29
  • 5
  • Hello Robert, welcome to SO. I would strongly recommend not using [`XML::Simple`](https://metacpan.org/pod/XML::Simple) per [the module's own suggestion](https://metacpan.org/pod/XML::Simple#STATUS-OF-THIS-MODULE). Instead, to avoid an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), paste the the actual XML code in your question instead of the parsed data structure to open up for much better answers. – Miller Aug 20 '14 at 17:48
  • Miller: Thanks! I now see that and will look at XML::libXML, and I will include the relevant code next time. Thanks again. – rj reilly Aug 20 '14 at 20:15

1 Answers1

0

You already converted the XML into perl data structure, so

use 5.010;
use warnings;
use Data::Dumper;

my $href = {
    "error"           => "EndOfResults",
    "model-responses" => {
        model => [
            {   attribute => { content => "wltvbswfc02", id => "0x1006e" },
                mh        => "0x100540",
            },
            {   attribute => { content => "wltvsutm1ds02", id => "0x1006e" },
                mh        => "0x100c80",
            },
            {   attribute => { content => "wltvsdora03", id => "0x1006e" },
                mh        => "0x100c49",
            },
        ],
    },
    "throttle"     => 86,
    "total-models" => 86,
    "xmlns"        => "http://www.ca.com/spectrum/restful/schema/response",
};

my %res = map { $_->{attribute}{content} => $_->{mh} }
    @{ $href->{"model-responses"}{model} };

say Dumper \%res;

The above prints:

$VAR1 = {
          'wltvsutm1ds02' => '0x100c80',
          'wltvsdora03' => '0x100c49',
          'wltvbswfc02' => '0x100540'
        };
Miller
  • 34,962
  • 4
  • 39
  • 60
clt60
  • 62,119
  • 17
  • 107
  • 194