Parsing XML

Here is a sample script that parses the following xml,

<notif_grps>
    <notifs>
        <email>joe@example.com</email>
        <email>sally@example.com</email>
        <email>mark@example.com</email>
    </notifs>
</notif_grps>

The following script will print out each email in the xml above in the order listed,

#!/usr/local/bin/perl

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
use CGI;

my $xml = new XML::Simple (ForceArray=>1);

my $xml_data = $xml->XMLin("path/to/notif_grps.xml");

my $cgi = new CGI;

print $cgi->header();

#print Dumper($xml_data);

for my $email (@{ $xml_data->{notifs}->[0]->{email} })
{
     print $email . " ";
}

The {notifs}->[0] part means we are reading the first <notifs> tag. In our example there is only one <notifs> tag, but if there was a second, you would reference it with {notifs}->[0].

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.