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!
In reply to Parse XML with Perl regex by ad23
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |