Solved

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

Posted on 2008-10-22
6
1,468 Views
Last Modified: 2012-06-22
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
Comment
Question by:Anu2117
[X]
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
  • 3
  • 3
6 Comments
 

Author Comment

by:Anu2117
ID: 22778639
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
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 22785744
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
 

Author Closing Comment

by:Anu2117
ID: 31508832
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
Independent Software Vendors: 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!

 
LVL 39

Expert Comment

by:Adam314
ID: 22787899

#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
 

Author Comment

by:Anu2117
ID: 22815307
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
 
LVL 39

Expert Comment

by:Adam314
ID: 22815526
You can use the XMLin function from the XML::Simple module.  I gave this comment in your other question.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

631 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