in reply to Create hash from XML file
use strict; use warnings; use XML::Twig; use Data::Dumper; my $xfile = <<EOF; <foo> <cat> <channelName>CHANNEL123</channelName> <name>CATEGORY123</name> <shortName>TopCategory</shortName> <description>This is TopCategory channel's Category</description> <shortDescription>This is TopCategory channel's Category</shortDescrip +tion> <parentID>0</parentID> <categoryTags>Vod;Download</categoryTags> </cat> <cat> <channelName>Channel456</channelName> <name>cat456</name> <shortName>TopCategory</shortName> <description>This is TopCategory channel's Category</description> <shortDescription>This is TopCategory channel's Category</shortDescrip +tion> <parentID>0</parentID> <categoryTags>Vod;Download</categoryTags> </cat> <cat> <channelName>chann678</channelName> <name>cat678</name> <shortName>TopCategory</shortName> <description>This is TopCategory channel's Category</description> <shortDescription>This is TopCategory channel's Category</shortDescrip +tion> <parentID>0</parentID> <categoryTags>Vod;Download</categoryTags> </cat> </foo> EOF my %cats; my $t= new XML::Twig( twig_handlers => {cat => \&cat} ); $t->parse($xfile); sub cat { my ($twig, $cat) = @_; $cats{$cat->first_child('channelName')->text()} = $cat->first_child +('name')->text(); } print Dumper(\%cats);
Prints out:
$VAR1 = { 'Channel456' => 'cat456', 'chann678' => 'cat678', 'CHANNEL123' => 'CATEGORY123' };
|
|---|