Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Create hash from XML file

by ashok13123 (Novice)
on Jul 22, 2009 at 11:11 UTC ( [id://782252]=perlquestion: print w/replies, xml ) Need Help??

ashok13123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I am having an XML like this sample.xml
<cat> <channel>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>
I am having an XML like this... I want to create a hash like
CHANNEL123=>CATEGORY123 Channel456=>cat456 chann678=>cat678
That is from each <cat>,I have to make channel=>name...How can i do that? Thanks in Advance

Replies are listed 'Best First'.
Re: Create hash from XML file
by dHarry (Abbot) on Jul 22, 2009 at 11:36 UTC

    The simplest seems using XML::Simple. This will give you a hash with the XML content. You can manipulate the hash as needed.

      Be sure to read the documentation. XML::Simple has several non obvious defaults that you'll probably want to override

      Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: Create hash from XML file
by toolic (Bishop) on Jul 22, 2009 at 12:19 UTC
    Very easily with XML::Twig. After fixing a typo in your posted XML code (and adding some phony 'foo' root tags):
    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' };
Re: Create hash from XML file
by prasadbabu (Prior) on Jul 22, 2009 at 11:45 UTC

    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

Re: Create hash from XML file
by Jenda (Abbot) on Jul 23, 2009 at 21:43 UTC
    use XML::Rules; my $parser = XML::Rules->new( rules => { __default => 'skip', 'channelName,name' => 'content', 'cat' => sub { return $_[1]->{channelName} => $_[1]->{name} }, 'rootTagName' => 'pass', } ); my $data = $parser->parse($xml);

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://782252]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 18:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found