For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > Getting number of children of the root in an XML object









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Getting number of children of the root in an XML object
Dave Adams

2006-03-31, 6:57 pm

I am trying to get the number of children of the root element and my script
gives me the number of children of the children instead.

Here is my XML document (ejournlist.xml):

<?xml version='1.0'?>
<EJOURNLIST>
<EJOURN>
<TI>Africa Confidential</TI>
<ISSUE>Vol. 1, no.1</ISSUE>
<FILEFORMAT>PDFformat</FILEFORMAT>
</EJOURN>
<EJOURN>
<TI>Time Magazine</TI>
<ISSUE>Vol. 1, no.1</ISSUE>
<FILEFORMAT>TXTformat</FILEFORMAT>
</EJOURN>
</EJOURNLIST>



Here is my perl script:

#!/usr/bin/perl -w
use XML::Simple;
#create object
my $xml = new XML::Simple;

#read XML file
my $data = $xml->XMLin("ejournlist.xml");

my $ejourn_cnt = scalar(keys %{$data->{'EJOURN'}});
print "The number of EJOURNs is: $ejourn_cnt \n";

*The scripts returns 3 when it should return 2.*

Any advice would be appreciated.

DA

**

Mr. Shawn H. Corey

2006-03-31, 6:57 pm

On Fri, 2006-31-03 at 15:42 -0500, Dave Adams wrote:
> I am trying to get the number of children of the root element and my script
> gives me the number of children of the children instead.
>
> Here is my XML document (ejournlist.xml):
>
> <?xml version='1.0'?>
> <EJOURNLIST>
> <EJOURN>
> <TI>Africa Confidential</TI>
> <ISSUE>Vol. 1, no.1</ISSUE>
> <FILEFORMAT>PDFformat</FILEFORMAT>
> </EJOURN>
> <EJOURN>
> <TI>Time Magazine</TI>
> <ISSUE>Vol. 1, no.1</ISSUE>
> <FILEFORMAT>TXTformat</FILEFORMAT>
> </EJOURN>
> </EJOURNLIST>
>
>
>
> Here is my perl script:
>
> #!/usr/bin/perl -w
> use XML::Simple;
> #create object
> my $xml = new XML::Simple;
>
> #read XML file
> my $data = $xml->XMLin("ejournlist.xml");
>
> my $ejourn_cnt = scalar(keys %{$data->{'EJOURN'}});


# What happens when you do this?
my $ejourn_cnt = scalar(keys %{$data->{'EJOURNLIST'}});

> print "The number of EJOURNs is: $ejourn_cnt \n";
>
> *The scripts returns 3 when it should return 2.*
>
> Any advice would be appreciated.
>
> DA
>
> **


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com