Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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


Comment on Re: Create hash from XML file
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://782261]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2014-08-31 11:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls