Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

XML::Simple - Answered !

by perldarren (Novice)
on Aug 30, 2012 at 11:55 UTC ( #990728=perlquestion: print w/ replies, xml ) Need Help??
perldarren has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Wondered if anyone could help me out please. I am using XML::Simple to parse a simple xml document. I would like to end up with a nice hash without any embedded arrays. This is a sample of the xml:
<overview execute_time="2012/08/30-10:38" xml_version="01-50-02" too +l_version="01-50-02" save_time="2012/08/30-10:37" dkc_type="RAID600" +micro_version="60-08-11-00/00" dkc_serial="17353" /> <host_grps number_of_ports="32"> <port port_id="CL1-A" number_of_host_grps="1"> <host_grp host_grp_name="1A-G00" host_grp_id="CL1-A-0"> <host_mode>LINUX</host_mode> </host_grp> </port> <port port_id="CL3-A" number_of_host_grps="1"> <host_grp host_grp_name="3A-G00" host_grp_id="CL3-A-0"> <host_mode>LINUX</host_mode> </host_grp> </port> ...
Here is my problem: I want the attribute host_grp_id="CL1-A-0" as a key so I execute:
my $ref = XMLin('host_grp.xml', KeyAttr => { host_grps => 'port', por +t => "port_id", host_grp => "host_grp_id" }, ForceArray => 'host_grp_ +id' );
gives me this:
$VAR1 = { 'overview' => [ { 'xml_version' => '01-50-02', 'save_time' => '2012/08/30-10:37', 'tool_version' => '01-50-02', 'execute_time' => '2012/08/30-10:38', 'dkc_serial' => '17353', 'micro_version' => '60-08-11-00/00', 'dkc_type' => 'RAID600' } ], 'host_grps' => [ { 'port' => { 'CL2-A' => { 'host_grp' => { 'CL2-A-0 +' => { + 'host_mode' => [ + 'LINUX' + ], + 'host_grp_name' => '2A-G00' + } }, 'number_of_host_grps' +=> '1' }, 'CL6-B' => { 'host_grp' => { 'CL6-B-0 +' => { + 'host_mode' => [ + 'LINUX' + ], + 'host_grp_name' => '6B-G00' + }, 'CL6-B-1 +' => { + 'host_mode' => [ + 'LINUX' + ], + 'host_grp_name' => 'schiphol_srs_gold' + } }, 'number_of_host_grps' +=> '2' },
But I don't want the 'ports' thrown into an array. If I drop ForceArray and do this instead:
my $ref = XMLin('host_grp.xml', KeyAttr => { host_grps => 'port', por +t => "port_id", host_grp => "host_grp_id" } );
I then get the following:
$VAR1 = { 'overview' => { 'xml_version' => '01-50-02', 'save_time' => '2012/08/30-10:37', 'tool_version' => '01-50-02', 'execute_time' => '2012/08/30-10:38', 'dkc_serial' => '17353', 'micro_version' => '60-08-11-00/00', 'dkc_type' => 'RAID600' }, 'host_grps' => { 'port' => { 'CL2-A' => { 'host_grp' => { 'host_mode +' => 'LINUX', 'host_grp_ +id' => 'CL2-A-0', 'host_grp_ +name' => '2A-G00' }, 'number_of_host_grps' => + '1' }, 'CL6-B' => { 'host_grp' => { 'CL6-B-0' +=> { + 'host_mode' => 'LINUX', + 'host_grp_name' => '6B-G00' + }, 'CL6-B-1' +=> { + 'host_mode' => 'LINUX', + 'host_grp_name' => 'schiphol_srs_gold' + } }, 'number_of_host_grps' => + '2' },
Notice that since CL2-A has only one 'host_grp' the host_grp_id which I would like to be a key is folded up making the key unavailable for query in my hash.
Am I just not understanding XML::Simple ?
Please help.
Regards,
Darren
It's ok, I answered my own question: my $ref = XMLin('host_grp.xml',KeyAttr => { host_grp => "host_grp_id", port => "port_id"}, ForceArray => host_grp); One has to ForceArray on the containing group host_grp and the world starts to look bright again. Thanks!

Comment on XML::Simple - Answered !
Select or Download Code
Replies are listed 'Best First'.
Re: XML::Simple - Answered !
by Jenda (Abbot) on Aug 31, 2012 at 11:53 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2015-07-30 13:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls