Sunday, March 18, 2012

A WSDL Client for a StockQuotes.wsdl file

When writing client apps for wsdl files the standard library for input/output alongwith parsers-wsdl, schema, and xml are put into use. The below program uses a WSDL c++ web services client library-WSDLPULL to create web services client for a StockQuotes.wsdl xml file. Methods such as setWSDLUri, setOperation, setValue and getValue insert and retrieve data from the xml and make it available to the client application.
/*
 * File:   WSDL_Client.cpp
 * Author: WSDL_Client
 *
 * Created on March 8, 2012, 2:21 AM
 */
#include <stdlib.h>
#include <WsdlInvoker.h>
#include <SchemaParser.h>
#include <XMLSerializer.h>
/*
 *
 */
int main(int argc, char** argv) {
WsdlInvoker invoker;
  if (!invoker.setWSDLUri("StockQuotes.wsdl")) {
    std::cerr<<invoker.errors()<<std::endl; return 2;
  }
 std::vector<std::string> ops;
  invoker.getOperations(ops);
  if(!invoker.setOperation("GetStockQuotes")){
      std::cerr<<"Error calling GetStockQuotes "<<std::endl;
      return 2;
   }
 if (invoker.status()){
       std::string ticker("XYZ");
       if (!invoker.setValue("QuoteTicker",(void*)(&ticker))){
            std::cerr<<"Incorrect input value "<<ticker<<std::endl;
            return 2;
       }
        if (invoker.invoke()){
         Schema::Type type;
         void *val = invoker.getValue("OpenPrice",type);
         //type is a string
         cout<<*((std::string*)val)<<std::endl;
   }
std::vector<std::string> stocks
     stocks.push_back("IBM");
     stocks.push_back("YHOO");
     stocks.push_back("MSFT");
     stocks.push_back("MOT");
     //4 occurrences of the <QuoteTicker> element
     invoker.setValue("QuoteTicker",stocks);
 int getNextInput(std::string & param ,Schema::Type & type,int & min,int & max);
    return (EXIT_SUCCESS);
}
<s:element name="GetQuotes"><s:complexType><s:sequence><s:element minOccurs="0" maxOccurs="1" name="QuoteTicker" type="s:string" />
</s:sequence></s:complexType></s:element><s:element name="GetQuotesResponse"> <s:complexType>
<s:sequence>   <s:element minOccurs="0" maxOccurs="1" name="GetQuotesResult" type="s0:ArrayOfQuote" />
</s:sequence>
</s:complexType></s:element><s:complexType name="ArrayOfQuote">
<s:sequence><s:element minOccurs="0" maxOccurs="unbounded" name="Quote" type="s0:Quote" />
</s:sequence>
</s:complexType><s:complexType name="Quote">
<s:sequence><s:element minOccurs="0" maxOccurs="1" name="CompanyName" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="StockTicker" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="StockQuote" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="LastUpdated" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="Change" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="OpenPrice" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="DayHighPrice" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="DayLowPrice" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="Volume" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="MarketCap" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="YearRange" type="s:string" />
</s:sequence></s:complexType><s:element name="ArrayOfQuote" nillable="true" type="s0:ArrayOfQuote" /></s:schema>
<message name="GetQuotesSoapIn"><part name="parameters" element="s0:GetQuotes" />
</message>
<message name="GetQuotesSoapOut"><part name="parameters" element="s0:GetQuotesResponse" /></message>
<portType name="StockQuotesSoap"><operation name="GetStockQuotes">
<input name="GetQuotes" message="s0:GetQuotesSoapIn" />
<output name="GetQuotes" message="s0:GetQuotesSoapOut" /></operation></portType>

No comments:

Post a Comment