ad23 has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to parse an XML file, with Perl regex (I know about the XML::Parse and ::Twig modules, but using regex is a requirement).
My XML document looks like this:
<?xml version="1.0"?> <t_volume> <info> <info_name>FZGA34177.b1</info_name> <center_project>4085729</center_project> <base_file>SETARIA_ITALICA/JGI/fasta/FZGA34177.b1.fasta</b +ase_file> <it_size>35000</it_size> <it_stdev>3500</it_stdev> <plate_id>357</plate_id> <program_id>KB 1.3.0</program_id> <seq_lib_id>FZGA</seq_lib_id> <project_id>32913</project_id> <info_archive> <ti>2167749207</ti> <taxid>4555</taxid> <basecall_length>899</basecall_length> <state>active</state> </info_archive> </info> <info> <info_name>FZGA34177.b1</info_name> <center_project>4085729</center_project> <base_file>SETARIA_ITALICA/JGI/fasta/FZGA34177.b1.fasta</b +ase_file> <it_size>35000</it_size> <it_stdev>3500</it_stdev> <plate_id>357</plate_id> <program_id>KB 1.3.0</program_id> <seq_lib_id>FZGA</seq_lib_id> <project_id>32913</project_id> <info_archive> <ti>2167749207</ti> <taxid>4555</taxid> <basecall_length>899</basecall_length> <state>active</state> </info_archive> </info> <info> <info_name>FZGA34177.b1</info_name> <center_project>4085729</center_project> <base_file>SETARIA_ITALICA/JGI/fasta/FZGA34177.b1.fasta</b +ase_file> <it_size>35000</it_size> <it_stdev>3500</it_stdev> <plate_id>357</plate_id> <program_id>KB 1.3.0</program_id> <seq_lib_id>FZGA</seq_lib_id> <project_id>32913</project_id> <info_archive> <ti>2167749207</ti> <taxid>4555</taxid> <basecall_length>899</basecall_length> <state>active</state> </info_archive> </info> <t_volume>
I have written the following code so far:
#!/usr/bin/perl my @files = glob('/abc*/info.xml') foreach my $xmlname(@xml) { open XML, $xmlname or die "Cannot open $xmlname for reading: $!\n" +; while($line=<XML>){ if($line=~ /\<info_name\>/i){ $info_name = $line =~ /\<info_name\>(\S+)\<\/info_name\>/i; } if($line=~ /\<it_size\>/i){ $it_size = $line =~ /\<it_size\>(\S+)\<\/it_size\>/i; } } print "$info_name : $it_size\n"; }
I want to get these values as a hash, with the data in <info_name> as key and that in <it_size> as value??
How to go about creating a hash for this??
Thanks in advance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parse XML with Perl regex
by graff (Chancellor) on Jul 07, 2010 at 22:13 UTC | |
|
Re: Parse XML with Perl regex
by graff (Chancellor) on Jul 07, 2010 at 22:24 UTC | |
by ad23 (Acolyte) on Jul 08, 2010 at 13:53 UTC | |
|
Re: Parse XML with Perl regex
by ikegami (Patriarch) on Jul 07, 2010 at 22:15 UTC | |
|
Re: Parse XML with Perl regex
by rowdog (Curate) on Jul 07, 2010 at 23:44 UTC | |
|
Re: Parse XML with Perl regex
by AndyZaft (Hermit) on Jul 07, 2010 at 21:51 UTC | |
by ad23 (Acolyte) on Jul 07, 2010 at 22:01 UTC | |
by almut (Canon) on Jul 07, 2010 at 23:11 UTC | |
by ad23 (Acolyte) on Jul 08, 2010 at 13:58 UTC | |
|
Re: Parse XML with Perl regex
by Anonymous Monk on Jul 07, 2010 at 21:56 UTC | |
| |
|