Perl (Scripting) - XML::Simple printing values (XMLin)

by
Jeremy Canfield |
Updated: March 09 2020
| Perl (Scripting) articles
Let's consider this XML.
<acme>
<name role="main">Bugs Bunny</character>
</acme>
Data::Dumper can be used to understand how XML::Simple is interpreting the XML. Here is an example of how to use Data::Dumper.
#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
my $xml= XMLin("example.xml");
print Dumper $xml;
Which will produce the following.
- "name" is the root key
- "content" and "id" are child keys below the root key
- "Bugs Bunny" is a value of the "content" key
- "1" is a value of the "id" key
- Data inside of { } is interepreted as a hash
$VAR1 = {
'name' => {
'content' => 'Bugs Bunny',
'role' => 'main'
}
};
In this example, "Bugs Bunny" will be printed.
print $xml->{'name'}->{'content'};
In this example, "main" will be printed.
print $xml->{'name'}->{'role'};
Did you find this article helpful?
If so, consider buying me a coffee over at