Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Create hash from XML file

by prasadbabu (Prior)
on Jul 22, 2009 at 11:45 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://782261]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-24 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found