Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Create hash from XML file

by prasadbabu (Prior)
on Jul 22, 2009 at 11:45 UTC ( #782261=note: print w/replies, xml ) Need Help??


in reply to Create hash from XML file

Hi ashok13123,

It is always best to use XML modules to parse these kind of files. Anyhow since it is simple one you can also use regex way.

Also in your xml file you have given in first <cat>

<cat> <channel>
I think it should be
<cat> <channelName>

Here is one way to accomplish your task.

use strict; use warnings; use Data::Dumper; my $input = " <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> "; my %hash; while ($input =~ /<channelName>((?:(?!<\/channelName>).)*)<\/channelNa +me>\s*<name>((?:(?!<\/name>).)*)<\/name>/gs){ $hash{$1} = $2; } print Dumper \%hash; output: ------- $VAR1 = { 'Channel456' => 'cat456', 'chann678' => 'cat678', 'CHANNEL123' => 'CATEGORY123' };

Prasad

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://782261]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2016-12-08 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (145 votes). Check out past polls.