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});
$parser->parsefile($file);

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?
kkbenjAsked:
Who is Participating?
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.

wilcoxonCommented:
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.
0
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?
0
kawasCommented:
my $file = <<EOF;
<?xml version="1.0" encoding="utf-8" ?>
<main>
  <scriptset>
    <name>Scriptset1</name>
  </scriptset>
  <server>
    <name>ch-kk-de</name>
  </server>
  <project>
  <name>Project1</name>
  </project>
  <database>
    <name>Ex1</name>
  </database>
  <table>
    <name>FirstTable</name>
    <field>
      <name>Field1</name>
      <data_type>long</data_type>
      <width>100</width>
      <format_string>0</format_string>
      <decimal_qty />
    </field>
    <field>
      <name>Field2</name>
      <data_type>string</data_type>
      <width>20</width>
      <format_string>""</format_string>
      <decimal_qty />
    </field>
    <field>
      <name>Field3</name>
      <data_type>long</data_type>
      <width>10</width>
      <format_string>0</format_string>
      <decimal_qty>2</decimal_qty>
    </field>
  </table>
  <process_controls>
    <post_delete_iloader_structure>Y</post_delete_iloader_structure>
    <post_delete_data_files>N</post_delete_data_files>
  </process_controls>
</main>
EOF

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";
0

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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

kkbenjAuthor Commented:
ABSOLUTELY perfect.  Exactly what is needed.

Thanks a million!
0
wilcoxonCommented:
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?).
0
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.
0
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
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.