Search This Blog

Tuesday, January 09, 2007

use XML::Simple

suppose I have a .passwds.xml file like this:


<?xml version="1.0"?>
<users>
<user>
<uid>0</uid>
<name>root</name>
<passwd>123456</passwd>
<identity>root</identity>
</user>
<user>
<uid>1</uid>
<name>test</name>
<passwd>123456</passwd>
<identity>root</identity>
</user>
</users>


and I can use the following perl script to parse it and determin whether a user can login


#!/usr/bin/perl
use XML::Simple;

$passwds = ".passwds.xml";

my $xmlobj = new XML::Simple(KeyAttr=>[]);

my $data = $xmlobj->XMLin("$passwds");

my $loggedin = -1;

my $username = "test";

my $password = "123456";

foreach (@{$data->{"user"}}){
if (($_->{"name"} ne $username)){
next;
}
$loggedin = 0;
if ($_->{"passwd"} eq $password){
$loggedin = 1;
}
last;
}
if ($loggedin == -1){
print "username incorrect\n";
die;
}
if ($loggedin == 0){
print "password incorrect";
die;
}
print "successfully login\n";

No comments: