ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-xml
perl-xml
RE: :Simple - how to iterate through records?
by Sterin, Ilya other posts by this author
Jul 10 2001 10:11PM messages near this date
view in the new Beta List Site
XML::Simple - how to iterate through records? | Getting XML::Twig to ignore certain elements?
>  -----Original Message-----
>  From: perl-xml-admin@[...].com
>  [mailto:perl-xml-admin@[...].com]On Behalf Of Phil Brodd
>  Sent: Tuesday, July 10, 2001 5:15 PM
>  To: Perl XML
>  Subject: XML::Simple - how to iterate through records?
> 
> 
>  I've been playing with XML::Simple, which seems quite nice.  What I don't
>  understand is how to refer to the entire array that is the value of the
>  'request' key below, or how to iterate through its elements.
> 
>  I am able to reference individual elements of the array with
>  $hashref->{request}->[index], but how do I refer to the whole thing?

What do you mean by "the whole thing"?  I am not sure

$hashref-> {request} is a reference to the whole thing.  Individual elements
are accessed through

$hashref-> {request}->[$index]->{section}->[0]

Ilya

> 
>  Thanks.
> 
>  Phil Brodd
> 
> 
> 
>  $VAR1 = {
>            'request' => [
>                           {
>                             'section' => [
>                                            '003'
>                                          ],
>                             'department' => [
>                                               '22C'
>                                             ],
>                             'webct_id' => [
>                                             'jblah'
>                                           ],
>                             'year' => [
>                                         '2002'
>                                       ],
>                             'course' => [
>                                           '017'
>                                         ],
>                             'repeating' => 'no',
>                             'termination_date' => '15-9-2002',
>                             'error_count' => '0',
>                             'session' => [
>                                            '3'
>                                          ],
>                             'email' => [
>                                          'joan-blah@the.edu'
>                                        ]
>                           },
> 
>                              .
>                              .
>                              .
>                         ]
>          };
> 
> 
> 
> 
>  use XML::Simple;
> 
>  my $xs = new XML::Simple(forcearray => 1,
>                              rootname => "autoreg",
>                              xmldecl => 1
>                          );
> 
>  my $hashref = $xs->XMLin('autoreg.xml');
> 
>  ## create a hash reference with the request data
>  my $request = {
>                  # attributes
>                  'repeating'           => 'yes',
>                  'termination_date'    => '15-9-2002',
>                  'error_count'         => '0',
>                  # child elements
>                  'department'          => ['009'],
>                  'user_id'             => ['jjack'],
>                  'year'                => ['2002'],
>                  'course'              => ['100'],
>                  'section'             => ['001'],
>                  'session'             => ['3'],
>                  'email'               => ['joe-jack@the.edu']
>                };
> 
>  $hashref->{request}->[2] = $request;
> 
>  my $xml = $xs->XMLout($hashref);
> 
> 
> 
>  Here is my XML:
> 
>  <?xml version='1' standalone='yes'?>
>  <autoreg>
>    <request repeating="no" termination_date="15-9-2002" error_count="0">
>      <section>003</section>
>      <department>22c</department>
>      <user_id>jblah</user_id>
>      <year>2002</year>
>      <course>017</course>
>      <session>3</session>
>      <email>joan-blah@[...].edu</email>
>    </request>
>    <request repeating="yes" termination_date="15-9-2002" error_count="0">
>      <section>009</section>
>      <department>08g</department>
>      <user_id>lyadda</user_id>
>      <year>2002</year>
>      <course>017</course>
>      <session>3</session>
>      <email>larry-yadda@[...].edu</email>
>    </request>
>  </autoreg>
> 
> 
> 
> 
>  _______________________________________________
>  Perl-XML mailing list
>  Perl-XML@[...].com
>  http://listserv.ActiveState.com/mailman/listinfo/perl-xml
_______________________________________________
Perl-XML mailing list
Perl-XML@[...].com
http://listserv.ActiveState.com/mailman/listinfo/perl-xml
Thread:
Phil Brodd
Sterin, Ilya

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved