Need help with programming assignment
So I have a programming assignment where I need to parse multiple XML files and return their attributes in a readable format. Currently the script parses all of the files and stores all the details for each file at each position of an array. An example of the desired output would be:
J MIG-49 at location "85,20,0.1" is moving to "0.5,-0.5,-300
Below is the XML code for an example.
Code:
<?xml version='1.0'?>
<threat>
<location>"85,20,0.1"</location>
<ftype>J</ftype>
<fmodel>MIG-49</fmodel>
<movement_speed>"0.5,-0.5,-300"</movement_speed>
</threat>
And here is my current Perl script.
Code:
my $count=0;
opendir (DIR, "../folder") or die "$!";
my @feeds = grep {/threats.*?\.xml/} readdir DIR;
close DIR;
# The above 3 lines read in the XML files and store them in array feeds.
for $threats(@feeds) {
open (READ, $threats)||die"$!";
flock(READ, 1) || die "Can't lock xml: $!";
while (<READ>) {
$all[$count].=$_
}
close READ;
$count++;
}
print @all; # report
Re: Need help with programming assignment
It doesn't look like you've done any parsing yet whatsoever. A simple Google search would have given you hundreds of helps...
http://oreilly.com/catalog/perlxml/chapter/ch03.html
http://www.techrepublic.com/article/...simple/5363190
Re: Need help with programming assignment
Quote:
Originally Posted by
PeejAvery
It actually does parse through. Those other examples are only for parsing a single XML file or a multi-level XML file. That's easy. This is multiple XML files within a loop.
Re: Need help with programming assignment
So then what's your problem if you already read through the files and already say that parsing XML is easy?
Re: Need help with programming assignment
Quote:
Originally Posted by
PeejAvery
So then what's your problem if you already read through the files and already say that parsing XML is easy?
My problem is that I haven't been able to find any example that are allowing me to pull out specific attributes from each file. Honestly I'm relatively new to the syntax of Perl so I'm not quite sure how I would do this. I need to pull out the values for location, ftype, fmodel, and movement_speed for each file as that is the relevant information needed in this program. Sorry if I've been too vague and I appreciate your help.
Re: Need help with programming assignment
Quote:
Originally Posted by
AngelBladeVII
My problem is that I haven't been able to find any example that are allowing me to pull out specific attributes from each file.
Which is the exact problem I covered with my first reply to you. And you said that you could already do it because it was easy. :confused:
Re: Need help with programming assignment
Quote:
Originally Posted by
PeejAvery
Which is the exact problem I covered with my first reply to you. And you said that you could already do it because it was easy. :confused:
I know the example there does that, but that's for only one file, as opposed to multiple files within a directory. I'm not quite sure how to set up a loop to do it for multiple files.
Re: Need help with programming assignment
Just put the parsing code inside of the loop. Then you have it executing on every file found.