XML::Parser using hash table

Using the following:
my $file = "C:\KJ\Test\test.xml";
my $inServer = 0;
my $inName = 0;
my $tag;

my $parser = new XML::Parser(
  Handlers => {Start => sub {$inServer = 1 if $_[1] eq 'server'; $inName = 1 if $inServer and $_[1] eq 'name'; $tag = $_[1];},
               End   => sub {$inServer = 0 if $inServer and $inName == 0; $inName = 0 if $inServer and $inName; $tag = undef;},
               Char  => \&PrintChar});

sub PrintChar {
        return unless $_[1] =~ /\S/;
        return unless $tag;
        print "$tag=$_[1]\n" if $inServer and $inName;

How can I use PrintChar to capture everything within the <field> tag in a hash table?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Given what you have and what you are looking for, you might find it much easier to capture what you want using XML::Simple (rather than XML::Parser).  The only real limitation on XML::Simple is that it doesn't work for very large XML files (it reads the entire thing into memory as a hash).

You create the hash by doing something as simple as "my $hash = XMLin($file, %options);".  There are plenty of options to somewhat change the default hash structure to one that works better for you.  You could then grab the server name by something like this:

foreach my $key (%{$hash->{tag1}{tag2}}) {
    print "server=", $hash->{tag1}{tag2}{server}{name}, "\n";

For your <field> tag, you just need to grab the hash directly out of the $hash from XMLin.
kkbenjAuthor Commented:
It has been many years since I've used Perl.  I copied your data into a file but nothing prints:
#!/usr/bin/perl -w

use XML::Simple;
my $file = "S:\\KJ\\Test\\test.xml";

#my $hash = XMLin($file, %options);
my $hash = XMLin($file);
foreach my $key (%{$hash->{tag1}{tag2}}) {
    print "server=", $hash->{tag1}{tag2}{server}{name}, "\n";
what did I do wrong?
my $file = <<EOF;
<?xml version="1.0" encoding="utf-8" ?>
      <decimal_qty />
      <decimal_qty />

use XML::Simple;
use Data::Dumper;

my $xs = XML::Simple->new();
my $ref = $xs->XMLin($file);
print Dumper($ref);

# access the server->name field
print "server name: ", $ref->{server}->{name}, "\n";

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

kkbenjAuthor Commented:
ABSOLUTELY perfect.  Exactly what is needed.

Thanks a million!
tag1 and tag2 were arbitrary tags thrown in because you did not provide an example of your XML so I did not know that server was a top-level (under root) element of your XML.

Please revise the point allocation as all kawas did was remove tag1 and tag2 from my code which he could do because he had your XML (from another question he helped you with?).
kkbenjAuthor Commented:
I could use a bit of help, one step further.  I will open a related question and assign you the points to fill one last gap of knowledge.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.