ankit.tayal560 has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; use Data::Dumper; use XML::DOM; my $parser=new XML::DOM::Parser; my $doc=$parser->parsefile('C:\perl\perl_tests\xmlin.xml') or die$!; my $config=$doc->getElementsByTagName("config"); my $config_attr=$config->getAttribute("logdir"); print("$config_attr");

XML file :<config logdir="var/log/foo/" debugfile="tmp/foo.debug"> <server name ="sahara" osname ="solaris" osversion="2.6"> <address>10.0.0.101</address> <address>10.0.1.101</address>Text </server> <server name="gobi" osname="irix" osversion="6.5"> <address>10.0.0.102</address>text1 </server> <server name="kalahari" osname="linus" osversion="2.0.34"> <address>10.0.0.103</address> <address>10.0.1.103</address>Text2 </server> </config> when I try to run this script I get an error saying : can't locate object method "getAttribute" via package "XML::DOM::NodeList" at C:\perl\perl_tests\xmlin.pl line 10. what is the problem with my script?

  • Comment on error : can't locate object method "getAttribute" via package XML::DOM::NodeList
  • Download Code

Replies are listed 'Best First'.
Re: error : can't locate object method "getAttribute" via package XML::DOM::NodeList
by Corion (Patriarch) on Sep 27, 2016 at 08:12 UTC

    Where do you find the method ->getAttribute documented for a XML::DOM::NodeList? It doesn't make sense to ask for the attributes of a list. Maybe you want to extract the first item from that list and ask for its attribute?

      http://search.cpan.org/~tjmather/XML-DOM-1.46/lib/XML/DOM.pm : in XML::DOM classes section goto XML::DOM::Element. there it is documented. and config is itself an element/tag infact it is the root element of the .xml file?

        Yes, that function makes sense and is documented for an XML::DOM::Element. But what ->getElementsByTagName returns is not a single XML::DOM::Element object. Please refer to the SYNOPSIS section of XML::DOM to see the correct usage.

Re: error : can't locate object method "getAttribute" via package XML::DOM::NodeList
by choroba (Cardinal) on Sep 27, 2016 at 08:45 UTC
    You can turn a node list into a Perl array by just dereferencing it as an array:
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use XML::DOM; my $parser = 'XML::DOM::Parser'->new; my $doc = $parser->parsefile(shift) or die $!; my $config = $doc->getElementsByTagName('config'); my $config_attr = $config->[0]->getAttribute('logdir'); say $config_attr;

    Update: code simplified.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,