Perl XML: : Dumper

From Teknologisk videncenter
Revision as of 11:16, 22 March 2009 by Heth (talk | contribs) (The client side)
Jump to: navigation, search

XML Dumper is simple and easy to use, yet it is very versatile.

Dumping hash'es

Had some problems dumping hashes trying to send the hash between a Server and Client, but it's really simple enough.

The server side

In this example I use XML::Dumper as an object.

 1 use XML::Dumper;
 2 my $dump = new XML::Dumper;
 3 
 4 my %clientorder = (	"LCnumber" => 7,
 5            		"LCsessions" => 10,
 6                         "loadServer" => "sun.tekkom.dk",
 7                         "loadPort" => 80);
 8 
 9 $xml = $dump->pl2xml(  \%clientorder  );
10 print $xml;

prints the following

<perldata>
 <hashref memory_address="0x28452d0">
  <item key="LCnumber">7</item>
  <item key="LCsessions">10</item>
  <item key="loadPort">80</item>
  <item key="loadServer">sun.tekkom.dk</item>
 </hashref>
</perldata>

I really don't know how to avoid the memory_address, but I have to use the reference slash in line 9, because pl2xml only works on scalars.
Now send the entire XML output to the Client.

The client side

In this example I use XML::Dumper as function calls.
NOTE: Assuming the scalar $xml contains the xml output from the server, described above.

1 use XML::Dumper;
2 my $c = xml2pl( $xml );    # Making a HASH reference
3 my %data=%$c;              # Dereferencing the hash reference
4 
5 foreach my $key(keys %data) {
6   print $key.":".$data{$key}."\n";
7 }

prints the following

loadPort:80
LCsessions:10
LCnumber:7
loadServer:sun.tekkom.dk

I really don't know how to avoid the memory_address, but I have to use the reference slash in line 9, because pl2xml only works on scalars.
Now send the entire XML output to the Client.