Hammy has asked for the wisdom of the Perl Monks concerning the following question:
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?<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>
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
Original title: 'array confusion'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem handling array from XML stream
by bobf (Monsignor) on Jun 05, 2006 at 03:27 UTC | |
by Thelonius (Priest) on Jun 05, 2006 at 03:30 UTC | |
by Anonymous Monk on Jun 06, 2006 at 00:08 UTC |