#!/usr/bin/perl -w use strict; use XML::Twig; use Data::Dumper; $|++; my $t = XML::Twig->new( #twig_roots => { 'Person' => 1}, # uncomment to dump entire XML in a hr form twig_handlers => { 'Person' => \&person }, pretty_print => 'indented', keep_encoding => 1, ); $t->parsefile('./File.xml'); $t->flush; sub person { my ($t, $section) = @_; # my $root = $section->root(); # uncomment do dump entire xml in a hr form my $id= $section->att('id'); my (@firstname, @middlename, @lastname, $description); my @para= $section->getElementsByTagName('Name'); foreach my $obj (@para) { if ($obj->att('NameType') eq 'Primary Name' ) { my $child = $obj->first_child('NameValue'); @firstname = $child->fields('FirstName'); @middlename= $child->fields('MiddleName'); @lastname = $child->fields('Surname'); } } my @list= $section->getElementsByTagName('Descriptions'); foreach my $obj (@list) { my $child = $obj->first_child('Description'); $description = $child->{'att'}->{'Description2'} if ($child->{'att'}->{'Description2'}); } print "$id,$firstname[0],$middlename[0],$lastname[0],$description\n" if ($description); }