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.

The xml file - Test.xml:
<?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"; }

Many thanks. -----
Of all the things I've lost in my life, its my mind I miss the most.

In reply to XML::Simple parsing by AcidHawk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.