FreeKB - Printing keys using XML::Simple in Perl
Printing keys using XML::Simple in Perl

Home > Search

Let's say you have an extremly simple XML file named cartoon.xml that contains the following markup.

  <name>Bugs Bunny</name>


In this example, "name" is the key and "Bugs Bunny" is the value.


The following script will allow you to parse the values from the cartoon.xml file.

use XML::Simple;
my $cartoon = XMLin("cartoon.xml");


You can print the keys . For example, the following markup will print "name" to the console.

print $_ for keys %$cartoon;


XML files can contains numerous "levels". Take for example the following XML.

        <name>Bugs Bunny</name>


In this scenario, to print "Bugs Bunny", you would need to include each "level" in your print statement.

print $_ for keys %{$cartoon->{'one'}->{'two'}->{'three'}};

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 2010d in the box below so that we can be sure you are a human.