scolife has asked for the wisdom of the Perl Monks concerning the following question:

Hello MONKS

I am consuming XML service using LWP. And I am parsing XML using XML::Simple. And I have to assign parsed variables to string. When I view variables in browser it shows normal.I am developing KOHA and it uses Template toolkit.

But when I add variables values to database (I am using mysql) it inserted as a HASH. How can I solve this?

My code is
my $rfid= $input->param('rfid'); my $ua = LWP::UserAgent->new; my $response = $ua->get("https:/mywebpage.com/studentservice/s +ervice.asmx/give_to_koha_byStudentCard?cardNR=$rfid"); my $xml=$response->content; # parse my $xs = new XML::Simple(keeproot =>0,searchpath => ".",forcea +rray => 1,); my $ref = $xs->XMLin($xml); my $firstname=${$ref->{firstname}}[0]; my $lastname=${$ref->{lastname}}[0]; my $sisiid=${$ref->{sisiid}}[0]; my $school=${$ref->{school}}[0]; my $program=${$ref->{program}}[0]; my $registerNr=${$ref->{registerNr}}[0]; my $userStatus=${$ref->{userStatus}}[0]; my $usertype=${$ref->{usertype}}[0]; my $photo=${$ref->{photo}}[0]; my $branch=${$ref->{abbrevm}}[0]; my $op= $input->param('op'); # save when op= "save" my %newdata; if($op eq "save"){ %newdata =("cardnumber"=>"$rfid", "surname"=>"$lastname", "firstname"=>"$firstname", "initials"=>"$registerNr", "address"=>"$program", "userid"=>"$sisiid", "categorycode"=>'ST', "branchcode"=>'EZS', "password"=>'aaaaaaa' ); my $borrowernumber=&AddMember(%newdata);
The main problem is assign parsed xml real value to variable.Not a HASH. Thank you.

Replies are listed 'Best First'.
Re: {KOHA}Parse XML and assign to variables.
by Corion (Patriarch) on Sep 15, 2015 at 11:58 UTC

    Does your code work if you eliminate LWP::UserAgent and XML::Simple?

    What are the values you get in $firstname, $lastname etc? What values get stored in the database?

    What does AddMember do?

    Most likely, the data structure you get back from XML::Simple is not what you expect. But as you don't show us the XML you download, it's hard to tell for us.

      I am expecting to get
      <to_user_info> <photo>http://webpage/getstpic.ashx?id=10010008460</photo> <lastname>asdasd</lastname> <firstname>asdasd</firstname> <sisiid>aB06B10253</sisiid> <school>aswe</school> <program>sw</program> <registerNr>89021800</registerNr> <phones/> <emails/> <userStatus> er</userStatus> <usertype>1</usertype> </to_user_info>
      If i eliminate LWP::UserAgent and XML::Simple from header section. It works.

        So, does it still work if you add XML::Simple in, without adding in LWP::UserAgent?

Re: {KOHA}Parse XML and assign to variables.
by Discipulus (Canon) on Sep 15, 2015 at 12:04 UTC
    just a tip: XML::Simple is deprecated as stated in the module documentation. Try switching to XML::Twig or other appreciated module to do xml parsing.

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: {KOHA}Parse XML and assign to variables.
by GotToBTru (Prior) on Sep 15, 2015 at 12:32 UTC

    Is AddMember code you wrote? If it is, that would be the place to start.

    If it is part of a module, I would look in the documentation for that module for details about what kind of input it expects. If that isn't clear, look in the code of the module itself.

    Dum Spiro Spero
      AddMember is written by koha developers. And I am trying to use this.

        And ... ? What does the KOHA documentation say about what the parameter(s) of AddMember should be?

        Dum Spiro Spero