The problem is that id , name , type and description handlers are executed for both occurrences. You will find that the contents of @fields matches @fields values, as the data from object values ββhas been overwritten.
In addition, when processing localcost elements localcost handlers cleaned up $ twig->, which removes data from memory. Therefore, when the localcosts handler localcosts called, it finds the item empty
I think the easiest way to do this is to write one handler that processes each object node at a time and then cleans it
This program demonstrates. Note that I used Data::Dumper only so that you can see the contents of @fields after it has been filled
It is very important that you use strict and use warnings at the top of every Perl program, especially if you ask for help. This is a simple measure that can reveal many simple errors that you otherwise might spend a lot of time searching.
Note also that the form of an "indirect object" of method calls is not recommended: you should write XML::Twig->new(...) instead of new XML::Twig (...) .
And if you use single quotes instead of double quotes, the backslash inside the string should not be doubled unless it is the last character of the string. But Perl is very happy if you use slashes as a path separator, even on Windows
I hope this helps
use strict; use warnings; use XML::Twig; use Data::Dumper; $Data::Dumper::Useqq = 1; my $twig= XML::Twig->new( twig_handlers => { object => \&get_Object }); my $lokaal = 'c:\temp\data3.xml'; my @fields; $twig->parsefile($lokaal); sub get_Object { my ($twig, $object) = @_; $fields[0] = $object->findvalue('id'); $fields[1] = $object->findvalue('name'); $fields[3] = $object->findvalue('type'); $fields[8] = $object->findvalue('description'); print Dumper \@fields; my @localcosts = $object->findnodes('localcosts/localcost'); for my $localcost (@localcosts) { my $type = $localcost->findvalue('type'); my $name = $localcost->findvalue('name'); my $description = $localcost->findvalue('description'); print "$type, $name, $description\n"; } $twig->purge; }
Output
$VAR1 = [ "VW-XJC9", "Name", undef, "House", undef, undef, undef, undef, "<p>some descrioption of the house</p> " ]; mandatory, What kind of cost, Some text again, different than the first tag