FreeKB - Perl (Scripting) XML::Simple store XML in a variable (XMLin)
Perl (Scripting) - XML::Simple store XML in a variable (XMLin)

At a high level, XML::Simple contains two main functions.

In this example, <p>Hello World</p> is store in a variable named $xml.

#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;

my $xml= XMLin("<p>Hello World</p>");

 

More commonly, an XML file is used. Let's say you have an XML file named foo.xml that contains the following.

<acme>
  <name>Bugs Bunny</name>
</acme>

 

Here is how you would store the contents of foo.xml in a variable named $xml.

#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;

my $xml= XMLin("foo.xml");

 

Data::Dumper can be used to print the $xml variable.

use Data::Dumper;
print Dumper $xml;

 

Something like this should be returned.

$VAR1 = { 'acme' => 
                    {
                      'name' => 'Bugs Bunny'
                    }
         };

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 180e4 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |