Subject: | find_node doesn't work with 0 values |
find_node cant find node with key value 0 if there is only one node of
mentioned name.
XML example:
---
<xml>
<ob>
<key>0</key>
<val>a</val>
</ob>
</xml>
---
Code example:
---
$object->find_node( $root->{xml}, 'ob', key => 0 );
---
This is because of different checks for single node and list of nodes.
From find_node method:
-----
if( ref( $node ) eq 'HASH' ) {
foreach my $key ( keys %match ) {
my $val = $match{ $key };
next if ( !$val ); # <-- seems it should be changed to (
!defined $val )
if( $node->{ $key }->{'value'} eq $val ) {
return $node;
}
}
}
if( ref( $node ) eq 'ARRAY' ) {
for( my $i = 0; $i <= $#$node; $i++ ) {
my $one = $node->[ $i ];
foreach my $key ( keys %match ) {
my $val = $match{ $key };
croak('undefined value in find') unless defined $val;
if( $one->{ $key }->{'value'} eq $val ) {
return $node->[ $i ];
}
}
}
}
-----
Wouldn't it be better to replace scalar with arrayref and work similarly
in both cases?
Something like:
-----
$node = [$node] if ref ($node) ne 'ARRAY';
... work with arrayref ...
-----
In additional - why not to use foreach to iterate through nodes?
Subject: | xml_bare_bug_1.pl |
#!/usr/bin/perl
use Data::Dumper;
use XML::Bare;
my $xml = <<EOF;
<xml>
<ob>
<key>0</key>
<val>a</val>
</ob>
</xml>
EOF
my $object_parse = new XML::Bare( text => $xml );
my $root = $object_parse->parse();
my $node = $object_parse->find_node( $root->{xml}, 'ob', key => '0' );
print Dumper( $node );