Hello, I have an XML stream that I am trying to parse and I am having a heck of a time properly handling the resulting array. Any suggestions would be greatly appreciated. Below is the XML:
<DOCUMENT NAME="SucceedWebGuideMaster" LANGUAGE="Default" FORMAT=" +2.0"> <Response> <Participant UID="JANEDOE" PID="8675309" FNAME="Jane" PROG +RAM_COUNT=3 DOB="03/26/76" /> <Program NAME="Balance" INSTRCD="blna"> <Status> <StatusCD>2</StatusCD> <StatusText>Newsletter 1</StatusText> <StatusDesc>nl1 has been published</StatusDesc> <StatusDate>04/20/05</StatusDate> <StatusEndDate> </StatusEndDate> </Status> <Status> <StatusCD>1</StatusCD> <StatusText>Plan</StatusText> <StatusDesc>mas has been published</StatusDesc> <StatusDate>04/20/05</StatusDate> <StatusEndDate>04/26/06</StatusEndDate> </Status> </Program> <Program NAME="Breathe" INSTRCD="blna"> <Status> <StatusCD>2</StatusCD> <StatusText>Newsletter 1</StatusText> <StatusDesc>nl1 has been published</StatusDesc> <StatusDate>04/20/05</StatusDate> <StatusEndDate> </StatusEndDate> </Status> <Status> <StatusCD>1</StatusCD> <StatusText>Plan</StatusText> <StatusDesc>mas has been published</StatusDesc> <StatusDate>04/20/05</StatusDate> <StatusEndDate>04/26/06</StatusEndDate> </Status> </Program> </Response> </DOCUMENT>
What I want to do is find out how many programs and how many statuses and walk through them. I used XMLin to break down this stream. I used the forcearray option to make sure I do not have to check for multiples (always an array). I put the following code together, but it does not work as I would have thought it would. What could I be missing?
use XML::Simple; use Data::Dumper; $XMLref = XMLin($xml_from_hm, suppressempty => '', forcearray=>1); if (ref ($XMLref->{Response}->[0]->{Program}->[0])) { @program_array = $XMLref->{Response}->[0]->{Program}; # Walk through the programs foreach $programs (@program_array) { @status_array = $programs->{status}; $one_program_name = $programs->{NAME}->[0]; # Walk through the statuses within each program foreach $status (@status_array) { $hold_desc = $status->{StatusDesc}->[0]; $hold_start = $status->{StatusDate}->[0]; $hold_end = $status->{StatusEndDate}->[0]; # a lot of other processing here } } }

2006-06-05 Retitled by planetscape, as per Monastery guidelines

( keep:2 edit:16 reap:0 )

Original title: 'array confusion'


In reply to Problem handling array from XML stream by Hammy

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.