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

Re: Problem handling array from XML stream

by bobf (Monsignor)
on Jun 05, 2006 at 03:27 UTC ( [id://553525]=note: print w/replies, xml ) Need Help??


in reply to Problem handling array from XML stream

First things first - I get a not well-formed error when I try to run your code. The fix is to quote the value of PROGRAM_COUNT in the Participant tag (PROGRAM_COUNT="3"). (Actually, that's the second error I got. The first thing I did was add use strict; use warnings; and then declare all of the variables.)

On to business. From what I can tell, you're getting confused about how to use references.

For example, in the following line you are assigning an array reference to an array, which is probably not what you intended to do.

my @program_array = $XMLref->{Response}->[0]->{Program};
Either expand (dereference) the array ref to an array when you assign it, or simply assign the reference.
my @program_array = @{ $XMLref->{Response}[0]{Program} }; my $program_aref = $XMLref->{Response}[0]{Program};

Secondly, you've got a typo in $programs->{status};. This key is capitalized in the XML ('Status').

Update: Thirdly, see Thelonius' reply to this node.

Here is a corrected version of the code:

if( ref( $XMLref->{Response}[0]{Program}[0] ) ) { my $program_arrayref = $XMLref->{Response}[0]{Program}; # Walk through the programs foreach my $programs_href ( @{ $program_arrayref } ) { my $status_aref = $programs_href->{Status}; my $one_program_name = $programs_href->{NAME}; # Walk through the statuses within each program foreach my $status_href ( @{ $status_aref } ) { my $hold_desc = $status_href->{StatusDesc}[0]; my $hold_start = $status_href->{StatusDate}[0]; my $hold_end = $status_href->{StatusEndDate}[0]; # a lot of other processing here print join( ':', $one_program_name, $hold_desc, $hold_start, $hold_end ), "\n"; } } }

This prints:

Balance:nl1 has been published:04/20/05: Balance:mas has been published:04/20/05:04/26/06 Breathe:nl1 has been published:04/20/05: Breathe:mas has been published:04/20/05:04/26/06

The following links may help:

HTH

Update:Thanks to Thelonius for pointing out a correction to the code that I made but neglected to mention. I guess I was fixing faster than I was documenting. :-)

Replies are listed 'Best First'.
Re^2: Problem handling array from XML stream
by Thelonius (Priest) on Jun 05, 2006 at 03:30 UTC
Re^2: Problem handling array from XML stream
by Anonymous Monk on Jun 06, 2006 at 00:08 UTC
    In the interests of TIMTOWTDI, I wrote up an XPath version of your script:
    use XML::LibXML; my $xml = XML::LibXML->new->parse_file('example.xml'); for my $status ($xml->findnodes('//Status')) { print join( ':', $status->findvalue('parent::Program/@NAME'), $status->findvalue('StatusDesc'), $status->findvalue('StatusDate'), $status->findvalue('StatusEndDate') ), "\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://553525]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-07-23 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.