Fiction Sci-Fi Detective History Modern Ancient #### #!/usr/bin/env perl use strict; use warnings; use XML::LibXML; my $parser = XML::LibXML::->new(); my $doc = $parser->load_xml(location => 'pm_1148640_xml_parse.xml'); get_category($doc); sub get_category { my ($doc) = @_; my @categories = $doc->findnodes('/library/category/name/text()'); push @categories, 'Exit'; my $selection = 0; print "Select a book category:\n"; print ++$selection, ". $_\n" for @categories; my $choice = get_user_selection({ map { $_ => undef } 1 .. $selection}); return if $choice == $selection; get_book($doc, $categories[$choice - 1]); } sub get_book { my ($doc, $category) = @_; my $xpath = "/library/category[name=\"$category\"]/book/title/text()"; my @books = $doc->findnodes($xpath); push @books, 'Back to Categories'; my $selection = 0; print "Select a $category book:\n"; print ++$selection, ". $_\n" for @books; my $choice = get_user_selection({ map { $_ => undef } 1 .. $selection}); if ($choice == $selection) { get_category($doc); return; } print "You selected the $category book: '$books[$choice - 1]'\n"; } sub get_user_selection { my $valid_selection = shift; my $selection = ''; while (1) { print 'Enter selection: '; chomp($selection = scalar <>); last if exists $valid_selection->{$selection}; print "Invalid selection [$selection]\n"; } return $selection; } #### $ pm_1148640_xml_parse.pl Select a book category: 1. Fiction 2. History 3. Exit Enter selection: 9 Invalid selection [9] Enter selection: 1 Select a Fiction book: 1. Sci-Fi 2. Detective 3. Back to Categories Enter selection: 3 Select a book category: 1. Fiction 2. History 3. Exit Enter selection: 2 Select a History book: 1. Modern 2. Ancient 3. Back to Categories Enter selection: 2 You selected the History book: 'Ancient'