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?
--------------------------
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?
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?
--------------------------
#!/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
}
--------------------------
what did I do wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ABSOLUTELY perfect. Exactly what is needed.
Thanks a million!
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?).
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?).
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.
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
}
For your <field> tag, you just need to grab the hash directly out of the $hash from XMLin.