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

by
Jeremy Canfield |
Updated: March 18 2021
| Perl (Scripting) articles
At a high level, XML::Simple contains two main functions.
- XMLin - store XML in a variable
- XMLout - write an XML file
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