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

Updated:   |  Perl (Scripting) articles

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'
                    }
         };

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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