Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found