AcidHawk has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I have a/an xml file with certain config info in it. I want to parse the xml file, search for the tag that contains the attribute that matches what I entered as param1 on the command line, and then return the contents of ALL the act tags associated with the matched header.
I have been working on this for a bit and have come up with the following xml file and code.
Looking at the code I feel like it belongs in the Obfuscation section though. Is there a simpler way of returning an array..? or $var for each match? I have to do something with each var anyway so which ever is easiest to maintain and read.
<?xml version="1.0"?> <!-- This is XML for the Header Mapping --> <map> <header name="ABCstatus"> <act>WTO</act> </header> <header name="ABCevent"> <act>WTO</act> <act>Call</act> </header> <header name="DEF-Call"> <act>Call</act> </header> <header name="Unknown"> <act>WTO</act> <act>SMS</act> <act>NetSend</act> </header> </map>
The hmmm don't even know what to call this code:
#! perl.exe use strict; use warnings; use XML::Simple; use Data::Dumper; my ($config, $head); unless ($head = $ARGV[0]) { my $str = "Usage: $0 name"; my $line = "-" x length($str); print "\n"; print "$str\n"; print "$line\n"; print "\n"; print "Setting Header as Unknown\n"; $head = "Unknown"; } my $file = "./Test.xml"; my $xs1 = XML::Simple->new(); unless ($config = $xs1->XMLin($file, forcearray => 1)) { print "Could NOT read $file IN:$!\n"; exit(); } my $hashrc = 0; print "\n"; #So I can see the structure while testing print Dumper($config); print "\n=======================================================\n"; print "Header = $head\n"; foreach my $key (keys (%{$config->{header}})) { my $tempkey = $config->{header}->{$key}->{'name'} . $key; print "\nTesting \$tempkey = $tempkey\n"; if ("$head" eq $config->{header}->{$key}->{'name'} . $key) { $hashrc = 1; foreach my $mapping (@{$config->{header}->{$head}->{act}}) { print "\$mapping = $mapping\n"; } last; } } unless ($hashrc) { print "$head is not currently mapped\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::Simple parsing
by mirod (Canon) on Oct 23, 2002 at 14:59 UTC | |
by AcidHawk (Vicar) on Oct 23, 2002 at 15:11 UTC | |
by mirod (Canon) on Oct 23, 2002 at 15:23 UTC | |
|
Re: XML::Simple parsing
by grantm (Parson) on Oct 24, 2002 at 01:02 UTC |