On 22/08/2014 11:27, angus wrote:
> Hi,
>
> Hi,
>
> I have some sample code that posts some XML content to an URL using
> LWP agent and XML Smart. The response to this comes back as some
> custom XML. I now need to try and parse the response and find the
> REDIRECTURL value and redirect the client to that destination.
>
> I am having issues parsing the output here is what I have tried.
>
> my $response = $ua->post($webpage, Content_Type => 'text/xml',
> Content => $message); $response->decoded_content;
>
> if ($response->is_success) { my $xml = new XML::Smart(
> "$resonse->content" , 'XML::Smart::HTMLParser' ); my $xml = new
> XML::Smart( "$resonse->content" , 'html' ); print "\n\n
> FOUND:$xml\n"; # comes back with a null value
>
> ########################################################## # another
> attempt below ###############################
>
> my $response = $ua->post($webpage, Content_Type => 'text/xml',
> Content => $message); $response->decoded_content;
>
> if ($response->is_success) { my $html = $response->content; while (
> $html =~/REDIRECT/g ) { print "I found: $1\n"; } else { print
> “nothing found\n”; };
>
> The find the value REDIRECT as the script prints out “I found: “ and
> not “nothing found” but I need the url value….
>
> Here is a sample of what the xml looks like that I am trying to
> parse.
>
> <PHC_LOGIN>\r <VERSION>3.0</VERSION>\r
> <PARTNERID>111111</PARTNERID>\r <USERLOGIN>John.doe</USERLOGIN>\r
> <ERROR_CODE>0</ERROR_CODE>\r <ERROR_DESCRIPTION>Login
> Successful</ERROR_DESCRIPTION>\r
> <REDIRECTURL>https://example.com/cust/login.asp</REDIRECTURL>\r
> </PHC_LOGIN>
There are a few problems here. I am concentrating on your first attempt,
as regular expressions are very rarely an appropriate way of processing XML.
- You need to save the content of the incoming message so that you can
pass it to the parser, but you have written
$response->decoded_content
in void context, so the result will be just thrown away
- In the line
my $xml = new XML::Smart( "$resonse->content" ,
'XML::Smart::HTMLParser' );
you mustn't put the method call in quotation marks.It will try to
stringify `$response` (which, incidentally, you have misspelled) and
result in something like
HTTP::Response=HASH(0x2c68544)->content
Plus, to be safe, you should be calling `decoded_content` instead of
just `content`.
- There is no need to specify a parser in the second parameter unless
you need specific behaviour, so that line should be
my $xml = XML::Smart->new( $resp->decoded_content )
after which you can simply access the elemetn you need using
$xml->{PHC_LOGIN}{REDIRECTURL}
I have put a semi-complete program below. It needs values for $webpage
and $message, but the rest of the code is in place, and it runs fine.
HTH,
Rob
use strict;
use warnings;
use 5.010;
use LWP;
use XML::Smart;
my $ua = LWP::UserAgent->new;
my $webpage = '';
my $message = '';
my $resp = $ua->post($webpage, Content_Type => 'text/xml', Content =>
$message);
my $xml = XML::Smart->new( $resp->decoded_content );
say $xml->{PHC_LOGIN}{REDIRECTURL};
**output**
https://example.com/cust/login.asp
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/