Problem with parsing XML file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AngelBladeVII
    New Member
    • Apr 2012
    • 1

    Problem with parsing XML file

    So I'm working on a program 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 for each line 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 reference.

    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
    As stated previously, I need to parse out the fields for each file as I'll need them later. I'm not quite sure how to do this though as I'm relatively new to Perl. Any help would be great.
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Take a look at these XML modules.

    XML::Simple - Easy API to maintain XML (esp config files)
    XML::Twig - A perl module for processing huge XML documents in tree mode.
    XML::LibXML - Perl Binding for libxml2
    More choices of XML modules

    Comment

    Working...