Howdo I Parse an Xml/Soap response and perform an action

I am receiving a simple xml/soap return from a HTTP post.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ATResponse xmlns="http://x.x.com/">
<ATResult>000000</AddTicketResult>
</ATResponse>
</soap:Body>

Where the ATResult can be -9 thru 8 digit number
the negative numbers are failures
the positive 6+ digit number is a success

I am using the following to code to submit (and it works)

#!/usr/bin/perl -w

use strict;

use LWP::UserAgent;
use HTTP::Request;

my $message = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
       stuff here
  </soap:Body>
</soap:Envelope>';

my $userAgent = LWP::UserAgent->new();
my $request = HTTP::Request->new(POST => 'http:/x.com/services/service.asmx');
$request->header(SOAPAction => '"http://x.com/spec_service"');
$request->content($message);
$request->content_type("text/xml; charset=utf-8");
my $response = $userAgent->request($request);

if($response->code == 200) {
      print $response->as_string;
}
else {
      print $response->error_as_HTML;
}


What I need to do is add some functionality.

1. I want to check the ATResult value
    if it is negative, then i want to get the value and print out the error to a log file
2. If it is the long number, I want to perform an external command on the server
        run a shell command and get the output into a variable (scalar) which
        then post the new data


How do I parse the ATResult?  What would the code look like?
How do I add the evaluation code for the parsed ATResult? What would the code look like?

I would greatly appreciate if you code provide specific code examples. Also, if I need a new perl module like XML::Parser, just let me know. I am totally new to per programming.

Thanks
bdorsey63Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
kawasConnect With a Mentor Commented:
Just to add to shivaspk's response:
use XML::LibXML;
use XML::LibXML::XPathContext; # not sure if this use is required ...

my $xml    = $response->as_string;
my $parser = XML::LibXML->new();
my $doc    = $parser->parse_string($xml);

my $xpath = "//*[local-name() = 'ATResult']";
my $xpc   = XML::LibXML::XPathContext->new();
my $nodes = $xpc->findnodes( $xpath, $doc->documentElement );

my $result = '';
$result = $nodes->get_node(1)->textContent if $nodes->size() >= 0;
print "atresult: ", $result, "\n";

Open in new window

0
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
You might want to look at SOAP::Lite
http://www.soaplite.com/
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
See if you can use a simple xpath

http://php.net/manual/en/simplexmlelement.xpath.php

with XPath as //*[local-name()='ATResult']
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
bdorsey63Author Commented:
xpath is a php code. It looks easy, but ..

I need it to work in perl.
0
 
kawasCommented:
the code i posted is perl
0
 
bdorsey63Author Commented:
Kawas,

Thanks.

I will  give it a try, and let you know.

0
 
bdorsey63Author Commented:
I tried the code and got

:1: parser error : Start tag expected, '<' not found.

I noticed that the actual string starts with a bunch of header stuff, I think I need to deal with it:

HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Date: Sun, 28 Mar 2010 20:40:33 GMT
Server: Microsoft-IIS/6.0
Content-Length: 364
Content-Type: text/xml; charset=utf-8
Client-Date: Sun, 28 Mar 2010 20:43:56 GMT
Client-Peer: 192.168.0.1:8080
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert  CA
Client-SSL-Cert-Subject: /C=US/ST=xxx/L=xxxx/O=Sns, Inc./OU=WEB/CN=*.xxxx.com
Client-SSL-Cipher: RC4-MD5
Client-SSL-Warning: Peer certificate not verified
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET

0
 
kawasConnect With a Mentor Commented:
do $response->content instead of $response->as_string
0
 
bdorsey63Author Commented:
Excellent that work.

use XML::LibXML;
use XML::LibXML::XPathContext; # not sure if this use is required ...

my $xml    = $response->content;
my $parser = XML::LibXML->new();
my $doc    = $parser->parse_string($xml);

my $xpath = "//*[local-name() = 'ATResult']";
my $xpc   = XML::LibXML::XPathContext->new();
my $nodes = $xpc->findnodes( $xpath, $doc->documentElement );

my $result = '';
$result = $nodes->get_node(1)->textContent if $nodes->size() >= 0;
print "atresult: ", $result, "\n";
0
 
bdorsey63Author Commented:
Thanks

Awesome.
0
All Courses

From novice to tech pro — start learning today.