#!/usr/bin/perl -w
use strict;
use XML::Parser;
my %hash;
my $depth = 0;
my @tags;
sub start
{
my ($expat, $element) = @_;
push(@tags, $element);
$hash{$tags[-1]} = '';
}
sub end
{
pop(@tags);
if (@tags == 1)
{
delete $hash{posts};
delete $hash{post};
# now you have hash.
print "Job: $hash{jobnumber}\n";
print "City: $hash{location}\n";
%hash = ();
}
}
sub char
{
my ($expat, $string) = @_;
$hash{$tags[-1]} .= $string;
}
my $text = <<'EOF';
1234
somecity NJ
87922
Othercity, AK
EOF
my $p1 = new XML::Parser(Handlers => { Start => \&start,
End => \&end,
Char => \&char });
$p1->parse("$text");