Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1477
  • Last Modified:

XML generation with XML::Simple::XMLout()

This question is continuation of another question.
http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_23831439.html

Currently I have the suggested code from the above ticket working, but I want to know if it's possible to have a xml file which looks a bit different than earlier.

for example:
This is what my current xml file looks like, But ,....
<opt>
  <nmi_0>
    <THREAD>0</THREAD>
    <TRIGGER>nmi_0.trigger</TRIGGER>
    <event>nmi</event>
    <nmi_0.trigger>
      <LIP>0x2020</LIP>
      <SELF_CHECK>'MUST_COMPLETE'</SELF_CHECK>
      <THREAD>0</THREAD>
      <trigger>eom</trigger>
    </nmi_0.trigger>
</opt>

I want my xml file to look like this.....  I was wondering if XML::Simple::XMLout() can do this?
 

<agent name=uei>
    <param name=nmi_0>
            <param name=THREAD>
                        <value val=0/>
            </param>
            <param name=TRIGGER>
                        <value val=nmi_0.trigger/>
            </param>
            <param name=nmi_0.trigger>
                        <param name=LIP>
                                    <value val = 0x02020/>
                        </param>
                        <param name=SELF_CHECK>
                                    <value val = MUST_COMPLETE/>
                        <param>
                         <param name=THREAD>
                                    <value val = 0/>
                        </param>
                        <param name = trigger>
                                    <value val=eom/>
                        </param>
            </param>
    </param>
</agent>


This is what my perl code looks like at present

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
 
#my @str = (";&EVENT[__nmi_0](nmi, THREAD => 0, TRIGGER => &TRIGGER[__nmi_0.trigger](eom, LIP => 0x2020, 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+)}$/) {
                        $Pieces{$piece}->{$k}=$1;
                        $Pieces{$piece}->{$1}=$Pieces{$2};
                  }
            }
      }
      if(/^;(.*):SubHash{(\d+)};$/) {
            $Event{$1}=$Pieces{$2};
      }
}
print XMLout(\%Event, keeproot=>1, NoAttr=>1);
 
sub MakeHash {
      return {lc($_[0])=> $_[2], split(/[,=>\s]+/, $_[3])};
}

0
Anu2117
Asked:
Anu2117
  • 3
  • 3
1 Solution
 
Anu2117Author Commented:
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));" );
This is an example string that needs to be converted to Hash first and then to the xml format.
I am trying to stick to xml::simple because that's what's installed on my machine. But if that won't be able to give me the xml output as I want, can someone suggest what xml library is needed to generate the desired xml output and what the library call should look like.
 
 
0
 
Adam314Commented:
I don't think the XML::Simple library will do what you want, nor will any of the other XML libraries.  You would have to manually process the hash.  Here is some code that has it's own version of XMLout.
#!/usr/bin/perl
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));");
 
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+)}$/) {
                        $Pieces{$piece}->{$k}=$1;
                        $Pieces{$piece}->{$1}=$Pieces{$2};
                  }
            }
      }
      if(/^;(.*):SubHash{(\d+)};$/) {
            $Event{$1}=$Pieces{$2};
      }
}
 
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;
}

Open in new window

0
 
Anu2117Author Commented:
This code works perfectly. Can you explain this below piece of code a little bit.
Can you please explain this piece of code.
1 while s/\&(\w+)\[__([^\]]+)\]\((\w+),\s+([^\(\)]+\)/print "String=$_\n";$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+)}$/) {
                        $Pieces{$piece}->{$k}=$1;
                        $Pieces{$piece}->{$1}=$Pieces{$2};
                  }
            }
      }
      if(/^;(.*):SubHash{(\d+)};$/) {
            $Event{$1}=$Pieces{$2};
      }
}
print Dumper(\%Event);
sub MakeHash {
      print "MakeHash(" . join("; ", @_) . ")\n";
      return {lc($_[0])=> $_[2], split(/[,=>\s]+/, $_[3])};
}

Thanks!!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Adam314Commented:

#Loop through each string in @str, set $_ to string value
foreach (@str) {
 
      #Clear %Pieces, define $Cnt
      %Pieces = ();
      my $Cnt;
      
      #The "1 while" mean continue doing this as long as possible
      #the s means search for a pattern, then replace it with something
      #the part between the first two / is what to search for
      #the part between the last two / is what to replace it with
      #the g on the end means global, repeat for all found patterns
      #the e on the end means the replace is an expression, as opposed to a string
      #This is the pattern to: \&(\w+)\[__([^\]]+)\]\((\w+),\s+([^\(\)]+)\)
      #  \&          The string '&'.  & is special, so it needs to be escaped
      #  (\w+)       1 or more word character, save this as $1
      #  \[__        The string '[__'.  [ is special so it needs to be escaped
      #  ([^\]]+)    1 or more non right bracket characters, saved as $2
      #  \]\(        The string ']('
      #  (\w+)       1 or more word character, save this as $3
      #  ,\s+        The string ',' followed by 1 or more spaces
      #  ([^\(\)]+)  1 or more of anything not a parenthesis, saved as $4
      #  \)          The string ')'
      #For replacing:
      #  increment the count
      #  for this piece, create a hash, save it in %Pieces
      #  replace the found part with the string "$2:SubHash{$Cnt}"
      1 while s/\&(\w+)\[__([^\]]+)\]\((\w+),\s+([^\(\)]+)\)/$Cnt++;$Pieces{$Cnt}=MakeHash($1,$2,$3,$4);"$2:SubHash{$Cnt}"/ge;
      
      # loop through all pieces, in reverse order
      for my $piece (sort {$b <=> $a} keys %Pieces) {
            # check each hash key/value pair for this piece
            while(my ($k, $v) = each %{$Pieces{$piece}}) {
                  #If this piece refers to some other piece, 
                  #create the reference to that piece
                  if($v =~ /^(.*):SubHash{(\d+)}$/) {
                        $Pieces{$piece}->{$k}=$1;
                        $Pieces{$piece}->{$1}=$Pieces{$2};
                  }
            }
      }
      #Create the top-level reference
      if(/^;(.*):SubHash{(\d+)};$/) {
            $Event{$1}=$Pieces{$2};
      }
}
 
#Print the %Event in dumper format, used for development/debugging
print Dumper(\%Event);
 
#Print the %Event in XMLout format
print XMLout(\%Event);
 
#From the 4 parts found in the pattern ($1, $2, $3, and $4),
#create and return a hash
sub MakeHash {
      return {lc($_[0])=> $_[2], split(/[,=>\s]+/, $_[3])};
}

Open in new window

0
 
Anu2117Author Commented:
Adam,
 
    Can you please show me how to parse this xml content back to a perl hash?? I posted another question with the same descrption.
http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_23851139.html
 
Thanks,
Anu.
0
 
Adam314Commented:
You can use the XMLin function from the XML::Simple module.  I gave this comment in your other question.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now