Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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!

In reply to XML::Simple - Answered ! by perldarren

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (14)
    As of 2015-07-06 15:19 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 (77 votes), past polls