CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
xml to an array


New User

Oct 19, 2009, 3:02 AM

Post #1 of 2 (1049 views)
xml to an array Can't Post

I have a script which picks up data, in the form of an XML response.

I want to read the response between to tabs <data> and </data> which is embedded inside <return><result> </result></return>

I cannot seem to get access to it.

Here is the code

 #!/usr/bin/perl -wuse strict; 
use LWP::UserAgent;
use HTTP::Request::Common;
use SOAP::Lite;
use XML::Simple;
use Data::Dumper;
use CGI::Carp qw/fatalsToBrowser/;
##use warnings;
# create object
my $xml = new XML::Simple;my $fieldsearch = 1;
my $fieldvalue = "9781416025405";
##my ($fieldsearch, $fieldvalue) = @ARGV;##print $fieldsearch, q{ -> }, $fieldvalue;
if ($fieldsearch eq "zero") {$fieldsearch =0;}my $message = "<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv='' xmlns:xsd=''><soapenv:Header/><soapenv:Body><xsd:getSearchService><xsd:param0><xsd:clientId>0</xsd:clientId><!--Optional:--><xsd:currency>GBP</xsd:currency><xsd:format>7</xsd:format><xsd:from>0</xsd:from><xsd:indexType>3</xsd:indexType><!--Optional:--><xsd:marketSegment>UK</xsd:marketSegment><!--1 or more repetitions:--><xsd:params><!--Optional:--><xsd:fieldLogic><xsd:fieldLogic>0</xsd:fieldLogic></xsd:fieldLogic><xsd:fieldSearch>"; my $FS = "</xsd:fieldSearch><xsd:fieldValue>"; my $FV = "</xsd:fieldValue></xsd:params><xsd:password>0</xsd:password><xsd:requestId>TestSearch</xsd:requestId> <xsd:resultView>1</xsd:resultView><!--1 or more repetitions:--><xsd:sortField><xsd:sortField>1</xsd:sortField><xsd:sortOrder>1</xsd:sortOrder></xsd:sortField><!--Optional:--><xsd:territory>UK</xsd:territory><xsd:to>20</xsd:to></xsd:param0></xsd:getSearchService></soapenv:Body></soapenv:Envelope>";
my $sendit= $message.$fieldsearch.$FS.$fieldvalue.$FV;my $userAgent = LWP::UserAgent->new();
my $request = HTTP::Request->new(POST => '');
$request->header(SOAPAction => '""');
$request->content_type("text/xml; charset=utf-8");
my $response = $userAgent->request($request);print "Content-Type: xml; charset=utf-8";
my $stuff = $response->as_string;
# read XML file
#my $data = $xml->XMLin($response); uncommenting this makes the script dieif($response->code == 200) {
# print output
print $response->as_string;
#print Dumper($data);}
else {
print $response->error_as_HTML;


Nov 11, 2009, 9:05 PM

Post #2 of 2 (977 views)
Re: [snoop] xml to an array [In reply to] Can't Post

:) i am hoping that you are just doing this for debugging or some kind of test script, if you are not, then use SOAP::Lite for all of your SOAP constructions, as it is not a good practice to just send soap call via useragent.

another thing, you attached code that is not even compiling, that is also bad

also you are posting to wsdl document an api call, that cant be right either

i looked at that provided wsdl, and it looks like it is very simple, so i would make a call via SOAP::WSDL

i dont have auth info, but here is how it would look:

use Data::Dumper;

my $soap = SOAP::WSDL->new(
wsdl => '',

my $result = $soap->call('getSearchService', {key => value});

where key value pairs are required parameters for your soap body.



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives