Convert xml to hash in perl

Posted on 2008-10-27
Last Modified: 2012-08-13
This is the continuation of

I was wondering if its possible to build a hash back from the xml that got generated. Will this hash match the original hash?

Basically,  I wanted to build a hash from a string and convert the hash to xml format. This is working so far with the code below. Now I want to see if it's possible to build a hash(hash of hash) back from this xml and will it look same as the original hash.

use strict;
use warnings;
use Data::Dumper;
my @str = (";&EVENT[__nmi_0](nmi, THREAD => 0, TRIGGER => &TRIGGER[__nmi_0.trigger](eom, LIP => 0x2020, SELF_CHECK => 'MUST_COMPLETE', THREAD => 0));",
          ";&EVENT[__psmi_1](psmi, THREAD => 0, TRIGGER => &TRIGGER[__retire_1](retire, LIP => 0x203f, TRIGGER => &EVENT[__smi_1](smi, THREAD => 1, TRIGGER => &TRIGGER[__eom_1](eom, LIP => 0x203f, THREAD => 0))));",
          ";&EVENT[__psmi_0](psmi, THREAD => 0, TRIGGER => &TRIGGER[__psmi_0.trigger](eom, LIP => 0x2000, SELF_CHECK => 'MUST_COMPLETE', THREAD => 0));" );

my %Event;
my %Pieces;
foreach (@str) {
      %Pieces = ();
      my $Cnt;
      1 while s/\&(\w+)\[__([^\]]+)\]\((\w+),\s+([^\(\)]+)\)/$Cnt++;$Pieces{$Cnt}=MakeHash($1,$2,$3,$4);"$2:SubHash{$Cnt}"/ge;
      for my $piece (sort {$b <=> $a} keys %Pieces) {
            while(my ($k, $v) = each %{$Pieces{$piece}}) {
                  if($v =~ /^(.*):SubHash{(\d+)}$/) {
      if(/^;(.*):SubHash{(\d+)};$/) {
print XMLout(\%Event);
sub MakeHash {
      return {lc($_[0])=> $_[2], split(/[,=>\s]+/, $_[3])};
sub XMLout {
      my ($ref, $pre) = @_;
      $pre='' unless defined($pre);
      my $str='';
      if(ref($ref) eq 'HASH') {
            foreach my $k (sort keys %$ref) {
                  $str .= "$pre<param name=$k>\n";
                  if(ref($ref->{$k})) { $str .= XMLout($ref->{$k},"$pre\t"); }
                  else { $str .= "$pre\t<value val=$ref->{$k} />\n"; }
                  $str .= "$pre</param>\n";
      else {
            die "Unsupported reference: " . ref($ref) . "\n";
      return $str;

Question by:Anu2117
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 39

Accepted Solution

Adam314 earned 500 total points
ID: 22815509
You can use XML::Simple to read the XML.
my $hash = XMLin($filename);

Open in new window

LVL 26

Expert Comment

ID: 25953867
I would suggest changing your script to use XML::Simple for both writing the XML and reading it back.  XML::Simple converts perl hash to XML (XMLout) and XML to perl hash (XMLin).

As Adam314 said, getting the perl hash from the XML is just:

my $hash = XMLin($filename);  # $filename can also be a string

I would suggest looking at the XML::Simple documentation as there are quite a few useful options to both XMLin and XMLout.

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question