#!/usr/bin/perl
=pod Notes and ToDo
Con-way XML Sample Code for Service Center Details Interface
required software: Expat (http://sourceforge.net/projects/expat/)
TODO:
* replace the USERNAME and PASSWORD String values below
with your Con-way username and password
Send questions to Con-way XML Support at web.support@Con-way.com
=cut
use strict;
use HTTP::Request::Common;
use LWP::UserAgent;
use Crypt::SSLEay;
use URI::Escape;
use XML::Parser;
use XML::SimpleObject;
=pod
Documentation for XML::SimpleObject here:
http://aspn.activestate.com/ASPN/CodeDoc/XML-SimpleObject/SimpleObject.html
=cut
## Replace USERNAME and PASSWORD strings with your Con-way username and password
my $username = 'USERNAME';
my $password = 'PASSWORD';
my $requestType = 'ServiceCenterDetailsRequest';
my $responseType = 'ServiceCenterDetailsResponse';
my $postUri = 'https://www.Con-way.com/webapp/servicecenter_app/ServiceCenterDetailsServlet';
=pod XML Data setup
In actual use, you would probably populate the parameters
from data submitted via an on-line order form or database.
For this sample we will just hard code some dummy data.
=cut
my $postalCode = "19348";
## Build the XML Request
my $xmlRequest = "
$postalCode
";
## print $xmlRequest;
## URL Encode the XML String
$xmlRequest = uri_escape($xmlRequest);
## Set up the HTTP request
my $req = HTTP::Request->new(POST => $postUri);
$req->authorization_basic($username, $password);
$req->content_type('application/x-www-form-urlencoded');
$req->content("$requestType=$xmlRequest");
my $userAgent = LWP::UserAgent->new;
my $resp = $userAgent->request($req);
## Abort now with error message if not successful
if (!$resp->is_success) {
print $resp->error_as_HTML;
exit;
}
## Parse the XML response
my $parser = XML::Parser->new(ErrorContext => 2, Style => "Tree");
my $respObj = XML::SimpleObject->new( $parser->parse($resp->content) );
my $rootEl = $respObj->child($responseType);
## Some ways to access the XML response data:
## To print out information for the entire XML response:
parseElement($rootEl);
## To get information for just one child element of the root:
my $respElement = "Name";
parseElement($rootEl->child($respElement));
## If you only want to get some key data:
my $respValue = $rootEl->child($respElement)->value;
print "\n $respElement: $respValue";
=item
This subroutine accesses and prints the XML tree for the element argument,
printing element names, attibutes and text values, but not in any order.
It processess recursively, so as to print the entire tree for the given element.
=cut
sub parseElement {
my $parseEl = $_[0];
print "Parsing element <", $parseEl->name, ">:\n";
printElementAttributes($parseEl);
printElementValue($parseEl);
foreach my $element ($parseEl->children()) {
print "<", $element->name, "> element - \n";
printElementAttributes($element);
printElementValue($element);
if ($element->children()) {
print " Beginning children of <",$element->name,">: \n\n";
parseElement($element);
print " End of children for <", $element->name, ">\n";
}
print "\n";
}
}
sub printElementAttributes {
my $element = $_[0];
if ($element->attributes()) {
my %attributes = $element->attributes();
print "\t", "Attributes: ";
foreach my $key (keys %attributes) {
my $value = $attributes{$key};
print $key."=\"".$value ."\" ";
}
print "\n";
}
}
sub printElementValue {
if ($_[0]->value) {
print "\t", "Value: ", $_[0]->value, "\n";
}
}