-
April 9th, 2012, 12:32 PM
#1
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
-
April 9th, 2012, 02:34 PM
#2
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
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
April 9th, 2012, 02:37 PM
#3
Re: Need help with programming assignment
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.
Last edited by AngelBladeVII; April 9th, 2012 at 03:15 PM.
-
April 9th, 2012, 03:49 PM
#4
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?
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
April 9th, 2012, 09:43 PM
#5
Re: Need help with programming assignment
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.
-
April 10th, 2012, 10:00 AM
#6
Re: Need help with programming assignment
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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
April 10th, 2012, 12:22 PM
#7
Re: Need help with programming assignment
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.
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.
-
April 10th, 2012, 01:02 PM
#8
Re: Need help with programming assignment
Just put the parsing code inside of the loop. Then you have it executing on every file found.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|