Link to home
Start Free TrialLog in
Avatar of gnagabandi
gnagabandi

asked on

XML-Parser for perl - How to work in windows environment.

How to download and set upthe XML::parser module. I am working in Windows 98 environment and using Active perl 5. Can any one help me by giving guidelines for Download and setup the module for windows environment. If it is not possible for windows environment, how can I download for UNIX environment?
 
Avatar of Kenny
Kenny
Flag of Malaysia image

ASKER CERTIFIED SOLUTION
Avatar of prakashk021799
prakashk021799

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gnagabandi
gnagabandi

ASKER

Thankyou very much prakash. But Iam not getting any sample scripts regarding how to use XML parser. My task is I have one XML file which contains the username and passwords. My CGI/Perl program takes the username and password from the client/user and checks the existence of that user in the XML file. If the user and password is present in the XML file then my perl do some other tasks.

Can You have any Idia or sample script regarding the above task. If you know any reference please help me.

Thanks inadvance for the help.
Here's a very bare-bones example. But it should give you some idea.

Assuming the password file is in this format:

<passwords>
    <user>
      <name>happy</name>
      <pass>sad</pass>
    </user>
    <user>
      <name>dopey</name>
      <pass>smart</pass>
    </user>
    <user>
      <name>doc</name>
      <pass>patient</pass>
    </user>
    <user>
      <name>sneezy</name>
      <pass>chirpy</pass>
    </user>
    <user>
      <name>bashful</name>
      <pass>noway</pass>
    </user>
</passwords>


Here's the script. Run it with three arguments: password file name, user name and password to check for.

#!/usr/local/bin/perl -w

use XML::Parser;

my ($file, $input_user, $input_pass) = @ARGV;

$xp = new XML::Parser(Handlers => {Start => \&start_handler,
                                   End   => \&end_handler,
                                   Char  => \&char_handler});

die "can't create XML::Parser object; $!\n"
    unless $xp;

my $found = undef;
$xp->parsefile($file);

if ($found) {
    print "Welcome, $input_user!\n";
} else {
    print "Sorry, $input_user!\n";
}

sub start_handler
{
    my ($xp, $elem) = @_;

#    print "Start: $elem\n";
    if ($elem eq 'name') {
        $user_seen = 1;
    } elsif ($elem eq 'pass') {
        $pass_seen = 1;
    }
}

sub end_handler
{
    my ($xp, $elem) = @_;

#    print "End: $elem\n";
    if ($elem eq 'name') {
        $user_seen = 0;
    } elsif ($elem eq 'pass') {
        $pass_seen = 0;
    }
}

sub char_handler
{
    my ($xp, $str) = @_;

#    print "    Value: $str\n";
    if ($user_seen) {
        $curr_user = $str;
    } elsif ($pass_seen) {
        $curr_pass = $str;
        check_pass($curr_user, $curr_pass)
            unless ($found);
    }
}

sub check_pass
{
    my ($user, $pass) = @_;

#    print "Checking $user/$pass against $input_user/$input_pass ...\n";
    $found = $user eq $input_user and $pass eq $input_pass;
}



Hope that helps.