sorry, here's a dumb simple question:
I am using the XML::Simple module to parse XML into a structure. This code works fine:
print "resultid:" . $response->{channel}->{result}->{resultid};
but assigning the value of the element has been difficult for me:
my $resultid = $response->{channel}->{result}->{resultid};
this does not work. I want to save the value of the has element as a scalar, or string, or whatever, for future use.
getting back into perl after a long absence. thanks in advance fer yer help.
fc
thanks, here's some code examples for your reference:
#all this works:
my $xmlsimple = XML::Simple->new();
my $response = $xmlsiple->XMLin($xml_content->content);
print "resultid:" . $response->{channel}->{result}->{resultid};
#this doesn't work:
my $resultid = $response->{channel}->{result}->{resultid};
print "resultid: " . $resultid;
the output looks like this:
resultid:
but should look like this:
resultid: The item was added to the database.
thanks again!
fc
OK, here is much more code for your review, thanks:
use XML::Simple;
use LWP::Simple;
use Data::Dumper;
use HTTP::Request::Common;
use LWP::UserAgent;
my $url = "http://localhost/xml/add_order.xml";
my $UserAgent = LWP::UserAgent->new(agent => 'perl post');
my $xml_content = $UserAgent->request(POST $url,["userid" => $userid
+,
"password" => $pass
+word,
"projectid" => $pro
+jectid,
"ordertypeid" => $o
+rdertypeid,
"orderdateid" => $o
+rderdateid,
"accountid" => $acc
+ountid,
"segcodeid" => $seg
+codeid,
"marketid" => $mark
+etid,
"marketday" => $mar
+ketday,
"marketmonth" => $m
+arketmonth,
"marketyear" => $ma
+rketyear,
"putcall" => $putca
+ll,
"strikeprice" => $s
+trikeprice,
"bs" => $bs,
"quantity" => $quan
+tity,
"orderprice" => $or
+derprice,
"entered_by" => $en
+tered_by
]);
print $xml_content->error_as_HTML unless $xml_content->is_success;
#if we want to see the XML
#printf $xml_content->content;
# also see ->code, ->is_info, ->is_success, ->is_error, -
+>header, ->message, etc
my $xmlsimple = XML::Simple->new( );
my $response = $xmlsimple->XMLin($xml_content->content);
#if we wanted to see the object as a structure:
#printf Dumper($response);
if ($response->{channel}->{error}->{errorid} == "") {
print "resultid: " . $response->{channel}->{result}->{resultid} .
+"\n";
print "resultdesc: " . $response->{channel}->{result}->{resultdesc
+} . "\n";
print "orderid: " . $response->{channel}->{result}->{orderid} . "\
+n";
my $resultid = $response->{channel}->{result}->{resultid};
+#ERROR
my $resultdesc = ${\$response->{channel}->{result}->{resultdesc}};
+ #ERROR
my $orderid = $response->{channel}->{result}->{orderid};
+#ERROR
printf "ERROR resultid:" . $resultid . "\n";
printf "ERROR: resultdesc:" . \$resultdesc . "\n";
printf "ERROR: orderid:" . $orderid . "\n\n";
# foreach my $tmp (@{$response->{channel}->{result}}) {
# print "resultid: " . $tmp->{resultid} . "\n";
# print "resultdesc: " . $tmp->{resultdesc} . "\n";
# print "orderid: " . $tmp->{orderid} . "\n";
# print "\n";
# }
} else {
print "errorid: " . $response->{channel}->{error}->{errorid} . "\n
+";
print "error desc: " . $response->{channel}->{error}->{errordesc}
+. "\n";
print "\n";
};
my @ret_arry = ("test",
$resultid,
$resultdesc,
$orderid);
printf "ret_arry0:" . $ret_arry[0] . "\n";
printf "ret_arry1:" . $ret_arry[1] . "\n";
printf "ERROR ret_arry2:" . $ret_arry[2] . "\n";
printf "ERROR ret_arry3:" . $ret_arry[3] . "\n\n";
#%ret_hash = (resultid => "a",
# resultdesc => "b");
return @ret_arry;
Grandfather, et al:
Here's a link with a static XML file that is created for my code:
http://frank.mazzy.com/xfer/tmp.xml
And here is output from the command line:
Adding order
resultid: 0
resultdesc: Success - Order 109 has been added to ProjectID 4217.
orderid: 109
ERROR resultid:0
ERROR: resultdesc:SCALAR(0x181187c)
ERROR: orderid:109
ret_arry0:test
ret_arry1:
ERROR ret_arry2:
ERROR ret_arry3:
array: test
arry0: test
arry1:
ERROR arry2:
ERROR arry3:
done
thanks again!
Edited by planetscape - added code tags