Categories

Tools

bitdefender

1&1 Web Hosting

Use Perl to read an XML configuration file


Suppose you have a file (settings.xml) like this :

Code:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="settings.xsl" ?>
<settings>
	<setting>
		<title>Use XML with Perl</title>
		<color>#8800aa</color>		
		<size>12</size>
		<height>90</height>
		<width>468</width>
	</setting>
</settings>

With Perl, you can read the contents of the XML file and use the values.

Code:

use XML::Simple;
use Data::Dumper;

# Create XML object
$xml = new XML::Simple;

# Load the xml data in $books
my $settings = $xml->XMLin("settings.xml");

# Print XML data
print uc("title: ") . "$settings->{setting}->{title} \n";
print uc("color: ") . "$settings->{setting}->{color} \n"; 
print uc("size: ") . "$settings->{setting}->{size} \n"; 
print uc("height: ") . "$settings->{setting}->{height} \n"; 
print uc("width: ") . "$settings->{setting}->{width} \n";

Result:

mdmsoft@linux-4nve:~/perl> perl xml.pl
TITLE: Use XML with Perl
COLOR: #8800aa
SIZE: 12
HEIGHT: 90
WIDTH: 468

Posted in Perl by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.