Problem while reading XML - TechRepublic
Question
January 5, 2008 at 04:07 AM
ravi.shankar

Problem while reading XML

by ravi.shankar . Updated 18 years, 5 months ago

Dear All :
I have an XML file like :

=========================================================


Kannel bearerbox version `1.4.0′.
Build `Mar 1 2005 10:23:13′, compiler `3.3.2 20031022 (Red Hat Linux 3.3.2-1)’.
System Linux, release 2.4.22-1.2199.nptl, version #1 Wed Aug 4 12:21:48 EDT 2004, machine i686.
Hostname s4.myvaluefirst.com, IP 207.234.209.215.
Libxml version 2.6.6.
Using OpenSSL 0.9.7a Feb 19 2003.
Compiled with MySQL 4.0.22, using MySQL 4.0.22.
Using native malloc.

running, uptime 8d 22h 23m 24s

00
00


7992950
41777457
-1
1.04
0.05

31981
internal


smsbox


127.0.0.1
0
on-line 8d 22h 22m 33s
no

smsbox


127.0.0.1
0
on-line 2d 22h 38m 24s
no

5


SMPP:61.8.156.50:7667/7667:ganesh:smpp
rsms1
re-connecting
0
0

0
0


SMPP:59.145.173.82:4576/4576:valuefrst:smpp
spice
re-connecting

9801
41777
5
0


SMPP:202.56.254.87:11612/11612:ValueFirst:vnin

airtelb
online 5541s
3770
0
0
0



SMPP:202.56.254.87:11612/11612:VFirst:smpp
airtelb1
online 5540s
358666
0

0
0


SMPP:202.56.254.87:11612/11612:ValueF:smpp
airtelb2
online 5541s

150
0
0
0



======================================================

And after parsing this XML using CPAN

I am getting the an array like this

======================================================
$VAR1 = {
‘smscs’ => {
‘count’ => ‘5’,
‘smsc’ => {
‘SMPP:202.56.254.87:11612/11612:VFirst:smpp’ => {
‘status’ => ‘online 5699s’,
‘id’ => ‘airtelb1’,
‘sent’ => ‘0’,
‘received’ => ‘358876’,
‘failed’ => ‘0’,
‘queued’ => ‘0’
},
‘SMPP:59.145.173.82:4576/4576:valuefrst:smpp’ => {
‘status’ => ‘re-connecting’,
‘id’ => ‘spice’,
‘sent’ => ‘41777’,
‘received’ => ‘9801’,
‘failed’ => ‘5’,
‘queued’ => ‘0’
},
‘SMPP:61.8.156.50:7667/7667:ganesh:smpp’ => {
‘status’ => ‘re-connecting’,
‘id’ => ‘rsms1’,
‘sent’ => ‘0’,
‘received’ => ‘0’,
‘failed’ => ‘0’,
‘queued’ => ‘0’
},
‘SMPP:202.56.254.87:11612/11612:ValueF:smpp’ => {
‘status’ => ‘online 5700s’,
‘id’ => ‘airtelb2’,
‘sent’ => ‘0’,
‘received’ => ‘150’,
‘failed’ => ‘0’,
‘queued’ => ‘0’
},
‘SMPP:202.56.254.87:11612/11612:ValueFirst:vnin’ => {
‘status’ => ‘online 5700s’,
‘id’ => ‘airtelb’,
‘sent’ => ‘0’,
‘received’ => ‘3771’,
‘failed’ => ‘0’,
‘queued’ => ‘0’
}
}
},
‘sms’ => {
‘inbound’ => ‘1.04’,
‘storesize’ => ‘-1’,
‘outbound’ => ‘0.05’,
‘sent’ => {
‘total’ => ‘41777’,
‘queued’ => ‘458’
},
‘received’ => {
‘total’ => ‘799506’,
‘queued’ => ‘0’
}
},
‘dlr’ => {
‘storage’ => ‘internal’,
‘queued’ => ‘31981’
},
‘status’ => ‘running, uptime 8d 22h 26m 3s’,
‘version’ => ‘Kannel bearerbox version `1.4.0\’.
Build `Mar 1 2005 10:23:13\’, compiler `3.3.2 20031022 (Red Hat Linux 3.3.2-1)\’.
System Linux, release 2.4.22-1.2199.nptl, version #1 Wed Aug 4 12:21:48 EDT 2004, machine i686.
Hostname s4.myvaluefirst.com, IP 207.234.209.215.
Libxml version 2.6.6.
Using OpenSSL 0.9.7a Feb 19 2003.
Compiled with MySQL 4.0.22, using MySQL 4.0.22.
Using native malloc.
‘,
‘boxes’ => {
‘box’ => [
{
‘ssl’ => ‘no’,
‘status’ => ‘on-line 8d 22h 25m 12s’,
‘IP’ => ‘127.0.0.1’,
‘queue’ => ‘0’,
‘id’ => {},
‘type’ => ‘smsbox’
},
{
‘ssl’ => ‘no’,
‘status’ => ‘on-line 2d 22h 41m 3s’,
‘IP’ => ‘127.0.0.1’,
‘queue’ => ‘0’,
‘id’ => {},
‘type’ => ‘smsbox’
}
]
},
‘wdp’ => {
‘sent’ => {
‘total’ => ‘0’,
‘queued’ => ‘0’
},
‘received’ => {
‘total’ => ‘0’,
‘queued’ => ‘0’
}
}
};

=====================================================
I have made a script to read this XML file

like

=====================================================

#!/usr/bin/perl

# use module
use XML::Simple;

# create object
$xml = new XML::Simple;

#dump XML
system(‘rm /root/perlscript/kannel’);
system (‘/usr/bin/perl /root/perlscript/openkannel > /root/perlscript/kannel’);

# read XML file
$data = $xml->XMLin(“kannel”);

# access XML data

print”$data->{smscs}->{smsc}->{hash_ref}->[2]->{name}”;

print”\nTotal is : $data->{sms}->{received}->{total}\n”;
print”\nStatus is : $data->{boxes}->{box}->{status}\n”;

#print”\n$data->{version}”;

print”\nTotal SMS received : $data->{sms}->{storesize}\n”;

print”\nTotal SMS : $data->{smscs}->{smsc}->{hash_ref}->{id}”;

===================================================
but I am getting error like

Bad index while coercing array into hash at ./checkkannel line 21.

can any one tell me how can I read and print following lines of array.

‘SMPP:202.56.254.87:11612/11612:VFirst:smpp’ => {
‘status’ => ‘online 5699s’,
‘id’ => ‘airtelb1’,
‘sent’ => ‘0’,
‘received’ => ‘358876’,
‘failed’ => ‘0’,
‘queued’ => ‘0’
},

Thanks And Regards
Ravi Shankar

This discussion is locked

All Comments