Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi everyone, I am rather new to perl and was hoping you could help me. I need to parse a file that's in an xml type format (not full xml syntax) and pull out data to run different types of jobs. The file looks like this:
<PROJECT_ID>12345</PROJECT_ID> <JOBID>101</JOBID> <TYPE1>add</TYPE1> <FILE1>/tmp/file_data_gros</FILE1> <JOBID>102</JOBID> <TYPE2>delete</TYPE2> <FILE2>/tmp/file_myvalues</FILE2>
The above file will always have a PROJECT_ID number and can have multiple JOBID fields defined in it. Each JODID will always have a corresponding TYPE* and FILE* associated with it. What I am trying to write is something that can loop through multiple files like this. So when each file is processed the code would pull out the PROJECT_ID field value and then for each JOBID (in order) it would pull out the corresponding values for TYPE* and FILE*. So in the above example the code would pull out the PROJECT_ID value and then get the value of the first JOBID seen in the file and once it has this it would then get the value for TYPE1 and FILE1 and then output a string in the following format with the values for each field:
PROJECT_ID(value) JOBID(value) TYPE1(value) FILE1(value)
.....I would then do some processing with these values. The loop would then carry onto the next JOBID seen and then output the values for these fields:
PROJECT_ID(value) JOBID(value) TYPE2(value) FILE2(value)
.....I would then do some processing with these values The loop would then carry on to the next JOBID seen and do the same until there are no more to process within this file
I am really struggling with how to go about doing this. I was thinking maybe I should read the whole file into a hash but I not too sure that this is the right approach. I have written this so far.
open FILE, "$file_to_process" or die; my %hash; while (my $line=<FILE1>) { chomp; (my $xmltag, $xmlvalue) = split /\<|\>/, $line; $hash{$xmltag} = $xmlvalue; }
If anyone can help me with some code that would be able to do what I need I would greatly appreciate it. My attempt is not working at all :-(
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing an file that has xml like syntax
by choroba (Cardinal) on Apr 02, 2014 at 18:58 UTC | |
by Anonymous Monk on Apr 02, 2014 at 19:20 UTC | |
by crusty_collins (Friar) on Apr 02, 2014 at 19:33 UTC | |
by Anonymous Monk on Apr 02, 2014 at 19:56 UTC | |
|
Re: Parsing an file that has xml like syntax
by GotToBTru (Prior) on Apr 02, 2014 at 18:53 UTC | |
by Anonymous Monk on Apr 02, 2014 at 19:09 UTC |