Link to home
Start Free TrialLog in
Avatar of kkbenj
kkbenj

asked on

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?
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

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.
Avatar of kkbenj
kkbenj

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of kawas
kawas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kkbenj

ASKER

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?).
Avatar of kkbenj

ASKER

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.