perl with xml : why to I need check if the Attribute is defined, shouldn't $node->attributes already do that? -
i'm trying use perl parse xml , i've run across seems odd. when call $node->attributes() seems return undefined value in cases. if @ line labeled problem, can see if i've added. have thought that if node had no attributes foreach wouldn't have had loop on. if uncomment if on line works. (i know put check outside loop, i'm wondering why need check @ all)
#!/usr/bin/perl use strict; use warnings; $filename = 'lib.xml'; use xml::libxml; $parser = xml::libxml->new(); $parser->keep_blanks(0); $doc = $parser->parse_file($filename); sub process_node { $level = shift; $node = shift; printf ("%*s", $level, ""); print $node->nodename; print "<", $node->nodevalue,">" if (defined($node->nodevalue)); print "\n"; print "attrs:\n"; foreach ($node->attributes()){ print $_->name,":",$_->value," " ;# if (defined($_)); ### problem } print "\n"; $child ($node->childnodes) { process_node($level+1, $child); } } process_node(1, $doc->documentelement);
here's contents of lib.xml:
<data size="4"> <stuff src="one" dst="two" /> hmm </data>
and here's "bad" output:
>>> ./xml.pl data attrs: size:4 stuff attrs: src:one dst:two text< hmm > attrs: can't call method "name" on undefined value @ ./xml.pl line 26.
and when uncomment if
>>> ./xml.pl data attrs: size:4 stuff attrs: src:one dst:two text< hmm > attrs:
this because of believe bug in xml::libxml::text::attributes
, looks this
sub attributes { return undef; }
this should be
sub attributes { return }
which return undef
in scalar context , empty list in list context
you can fix if want. in latest version of module -- 2.0123 -- it's on line 1789 of libxml.pm
in xml::libxml
installation. can discover file running
perldoc -l xml::libxml
i raise bug report maintainers
Comments
Post a Comment