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: Package to include in order to use getLength function in XML::DOM parser
by Grant McLean other posts by this author
Nov 13 2006 8:41PM messages near this date
view in the new Beta List Site
Package to include in order to use getLength function in XML::DOM parser | Perl Editor
& XSLT On Mon, 2006-11-13 at 19:12 -0500, Jaebin Lee wrote:
>  Hi,
>  I'm just trying to use the getLength function off of a xml node, but
>  get an error when used it.
>  
>  the error is saying: "can't call method "getLength" without a package
>  or object reference"
>  
>  and this is part of my codes.

Your script is missing:

  use strict;
  use warnings;

Which would have picked up the error for you.

>  my @root = $doc->getElementsByTagName("environmentConfig");
>  my $length = @root->getLength;

You can't call the getLength method on an array.

The getElementsByTagName method is sensitive to whether it is called in
scalar or list context.  You've called it in list context, so to get the
number of elements returned, you just need to know the length of the
list:

  my $length = @root;

Alternatively, if you call the method in scalar context, you get a
NodeList object rather than an array of matching nodes.  You can call
getLength on that object:

 my $root = $doc-> getElementsByTagName("environmentConfig");
 my $length = $root-> getLength;

But having said all that, you obviously haven't got very far with your
project yet, so now would be a good time to switch to using XML::LibXML
rather than XML::DOM.  The XML::LibXML module supports XPath and that
will make things much easier for you.

Regards
Grant

_______________________________________________
Perl-XML mailing list
Perl-XML@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Jaebin Lee
Grant McLean

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