Printing values using XML::Simple in Perl

Home > Search
  by

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

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

 

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.

#!/usr/bin/perl
use XML::Simple;
$cartoon = XMLin("cartoon.xml");

 

You can print the values associated with certain XML tags. For example, the following markup will print the value of the name tag. This will print Bugs Bunny to the console.

print $cartoon->{'name'};

 

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

<acme>
  <one>
    <two>
      <three>
        <name>Bugs Bunny</name>
      </three>
    </two>
  </one>
</acme>

 

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

print $cartoon->{'one'}->{'two'}->{'three'}->{'name'};

 


Multiple Values / Arrays

Consider the following XML. 

<acme>
  <main>
    <name>Bugs Bunny</name>
    <name>Elmer Fudd</name>
    <name>Yosemite Sam</name>
  </main>
</acme>

 

When there are two or more identical keys at the same location in an XML file, XML::Simple interprets this as an array. The following Perl script will fail to print the values of the name key.

print $cartoon->{'main'}->{'name'};


You can print a particular cartoon character by specifying the array value (0 prints Bugs Bunny, 1 print Elmer Fudd, 2 prints Yosemite Sam). 

print $cartoon->{'name'}->[0];
print $cartoon->{'name'}->[1];
print $cartoon->{'name'}->[2];

 

Or, you could print all of the values in the array. This will print Bugs Bunny Elmer Fudd Yosemite Sam. However, this will not work if there is only a single cartoon character.

print @{$cartoon->{'name'}};

 

The solution to this challenge is to use the ForceArray => 1 option.

$cartoon = XMLin("cartoon.xml", ForceArray => 1 );

 

Once the ForceArray => 1 option is used, you can then use the following to print all of the values of the array. This will work when there is only a single item in the array, or when there are two or more items in the array.

print @{$cartoon->{'name'}};

 

One caveot is that the following markup will fail to print a cartoon character once the ForceArray options is set.

print $cartoon->{'name'};

 

Instead, you would have to append the array value, like this.

print $cartoon->{'name'}->[0];

 


Multiple arrays

Let's say your cartoon.xml file contains more than one array. 

<acme>
  <characters>
    <name>Bugs Bunny</name>
    <name>Elmer Fudd</name>
    <name>Yosemite Sam</name>
  </characters>
  <creators>
    <name>Tex Avery</name>
    <name>Chuck Jones</name>
    <name>Robert McKimson</name>
  </creators>
</acme>

 

The following will print the values of each array.

print @{$cartoon->{'characters'}->[0]->{'name'}};
print @{$cartoon->{'creators'}->[0]->{'name'}};

 



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




Comments