Skip to content

Instantly share code, notes, and snippets.

@TyrfingMjolnir
Forked from izzuddin91/swift_soap_api.swift
Created January 20, 2019 21:26
Show Gist options
  • Select an option

  • Save TyrfingMjolnir/06d8bb6acadfd94e26c8bd8d45a1f4cc to your computer and use it in GitHub Desktop.

Select an option

Save TyrfingMjolnir/06d8bb6acadfd94e26c8bd8d45a1f4cc to your computer and use it in GitHub Desktop.
swift xcode xml parser soap ui
//
// TestViewController.swift
// etiqa-autoclaim
//
// Created by izzuddin on 25/10/2017.
// Copyright © 2017 etiqa. All rights reserved.
//
import UIKit
import Foundation
class TestViewController: UIViewController {
var posts = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
var title1 = NSMutableString()
var resutlsArray = NSMutableArray()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let headers = [
"content-type": "text/xml"
]
/*
let string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-SEC=\"http://schemas.xmlsoap.org/soap/security/2000-12\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> <soapenv:Header> <SOAP-SEC:Signature soapenv:actor=\"\" soapenv:mustUnderstand=\"0\"> <ds:Signature xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\"> <ds:SignedInfo> <ds:CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /> <ds:SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" /> <ds:Reference URI=\"#Body\"> <ds:DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /> <ds:DigestValue>CFLJheqwtVv2hlKFvGDm9Oi+eOM=</ds:DigestValue> </ds:Reference> </ds:SignedInfo> <ds:SignatureValue>lMLyPVQLMziefcLBWJGoeOmOCKw1wKJJ6BJaFeVbHEF/1oDEkDCC3ZH0rywV7es38JUOaG2P0fCt snXApMmMgT0gTaCtqvfl3HPxCMWck4tt2x5qgMhc+aTYX1IMy1AE3utMV2n3iTigjjuMBTYnW0+M XOeJRqNuOSX1zmjtQNE=</ds:SignatureValue> </ds:Signature> </SOAP-SEC:Signature> </soapenv:Header> <soapenv:Body Id=\"Body\"> <CalMotorRequest soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> <DETAFFReq href=\"#id0\" /> </CalMotorRequest> <multiRef xmlns:ns1=\"urn:DETAFFServices\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" id=\"id0\" soapenc:root=\"0\" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"ns1:CalMotorRequest\"> <inputFieldName xsi:type=\"soapenc:string\">FI_Motor_Src^A_Motor_TransactionType^A_Motor_Con_Tak^A_Insured_Staff_IND^A_Insured_Staff^A_Insured_Email^A_Insured_IDType^A_Insured_CustomerType^A_Insured_NewIC^A_Insured_OldIC^A_Insured_BusRegNo^A_Insured_Passport^A_Insured_DOB^A_Insured_Gender^A_Insured_Occupation^A_Insured_MaritalStatus^A_Insured_Blacklist^A_Insured_Race^A_Policy_No^A_Quotation_No^A_Policy_Ref_No^A_Risk_No^A_Motor_Agent_Code^A_Motor_Agent_Group^A_Motor_Agent_Type^A_Motor_Agent_SubType^A_Motor_ProductType^A_Motor_TransactionDate^A_Motor_CoveragePeriod_Start^A_Motor_CoveragePeriod_End^A_Driver_EtiqaProducts^A_Driver_FirstMotorPolicy_InceptionDate^A_Motor_PaymentFrequency^A_Motor_CO_IN_IND^A_Motor_MultiRisk_IND^A_Motor_Endorsement_Effective_Date^A_Motor_TotalLossRatio^A_Motor_ManualReview_IND^A_Motor_Scheme_IND^A_Motor_Commission^FI_Motor_Ver^FI_DSA_Timestamp^A_Motor_Insured_LicenseType^A_Motor_FirstRisk_IND^A_Motor_VehicleRegNo^A_Motor_ChassisNo^A_Motor_BlacklistedVehicle^A_Motor_VehicleRegStatus^A_Insured_DriveExp^A_Motor_Region^A_Motor_SumInsured^A_Motor_VehicleMarketValue^A_Motor_VehicleAgreedValue^A_Motor_HirePurchase^A_Motor_HirePurchase2^A_Motor_RiderType^A_Motor_VehicleClass^A_Motor_Usage^A_Motor_ChangeofVehicleUse^A_Motor_Coverage^A_Motor_Body^A_Motor_NVIC_Code^A_Motor_NVIC_Rating^A_Motor_Make^A_Motor_Model^A_Motor_SeatCapacity^A_Motor_EngineCapacity^A_Motor_Carriage_Type^A_Motor_YearofManufacture^A_Motor_OvernightParking^A_Motor_Postcode^A_Motor_Area^A_Motor_State^A_Motor_SafetyFeatures^A_Motor_AntiTheft^A_Motor_VoluntaryExcess_Type^A_Motor_Excess^A_Motor_VoluntaryExcess^A_Motor_TargetPremium^A_Motor_Discount_Code1^A_Motor_Discount_Percentage1^A_Motor_Discount_Code2^A_Motor_Discount_Percentage2^A_Motor_Campain_Code^A_Motor_LatePayment^A_Motor_Short_ProRate^A_Motor_FAC_RI_IND^A_Motor_AnnualPremium_UW^A_Motor_TransferOwner_IND^A_Motor_ModifiedEngine^A_Motor_Loading_Percentage^A_Motor_DriverPlate_IND^A_Motor_DriverPlate_Value^A_Motor_Longitude^A_Motor_Latitude^A_Motor_PML^A_Motor_Channel^A_Motor_RatedDriverID^A_Motor_AP_BNCD^A_Motor_HPFV_IND^A_Motor_HotModel_IND^A_Motor_PickUp_IND^A_Motor_SportsCar_IND^A_Motor_RenewLetter_IND^A_Motor_AgentGST_IND^A_Motor_CampaignSDate^A_Motor_CampaignEDate^A_Motor_Campain_Perc^A_Motor_Campain_Amt^A_Motor_LastYrCalc_IND^A_Motor_LastYrLoadPerc^A_Motor_AdditionalBenefits_Inclusion^A_Motor_AdditionalBenefits_Code^A_Motor_AdditionalBenefits_Amount^A_Motor_AdditionalBenefits_Limit^A_Motor_AdditionalBenefits_Start^A_Motor_AdditionalBenefits_End^A_Motor_PolHolder_NCD^A_Motor_NCD_EffectiveDate^A_Motor_NumClaimsPast3years^A_Motor_Lapsed_Days^A_Motor_Convicted^A_Motor_AllClaimsPast3years^A_Motor_WSClaimsPast3years^A_Driver_DOB^A_Driver_DrivingYears^A_Driver_ClaimHistory^A_Driver_MajorConvictions^A_Driver_LicenseType^A_Driver_Gender^A_Driver_MaritalStatus^A_Driver_Race^A_Motor_UW_Questions^A_Motor_UW_Answers^A_Motor_State_Grp^A_Motor_FE_Reserved0^A_Motor_FE_Reserved1^A_Motor_FE_Reserved2^A_Motor_FE_Reserved3^A_Motor_FE_Reserved4^A_Motor_FE_Reserved5^A_Motor_FE_Reserved6^A_Motor_FE_Reserved7^A_Motor_FE_Reserved8^A_Motor_FE_Reserved9^A_Motor_FE_Reserved10^A_Motor_FE_Reserved11^A_Motor_FE_Reserved12^A_Motor_FE_Reserved13^A_Motor_FE_Reserved14^A_Motor_FE_Reserved15^A_Motor_FE_Reserved16^A_Motor_FE_Reserved17^A_Motor_FE_Reserved18^A_Motor_FE_Reserved19^A_Motor_DDM_Reserved0^A_Motor_DDM_Reserved1^A_Motor_DDM_Reserved2^A_Motor_DDM_Reserved3^A_Motor_DDM_Reserved4^A_Motor_DDM_Reserved5^A_Motor_DDM_Reserved6^A_Motor_DDM_Reserved7^A_Motor_DDM_Reserved8^A_Motor_DDM_Reserved9^A_Motor_DDM_Reserved10^A_Motor_DDM_Reserved11^A_Motor_DDM_Reserved12^A_Motor_DDM_Reserved13^A_Motor_DDM_Reserved14^A_Motor_DDM_Reserved15^A_Motor_DDM_Reserved16^A_Motor_DDM_Reserved17^A_Motor_DDM_Reserved18^A_Motor_DDM_Reserved19</inputFieldName> <inputFieldValue xsi:type=\"soapenc:string\">POLM^NB^1^N^^^001^P^490827075233^^^^19490827^002^UNKN01^001^N^001^V0653580^PM12017M-V0653580-004^00001^0001^V0022314^^RDC^^CVPRVP^20170707^20170808^20180501^000000^99999999^D000^N^N^20170502^0000000000^N^N^00000000^^^L^Y^RE564^FREG21654^N^NA^000^W^00000023000^00000023000^N^^^^PV^PT^N^CO^^H7Q05A^^25^42^0^0^C^2005^G1 ^78000^78000^^S99^T10^^ ^ ^ ^^ ^^000000^^0000000000^P^N^0000000000000+^N^N^^^00^000000000+^000000000+^0000000000^00000000^99999999^0000000000000+^N^Y^N^N^^N^99999999^99999999^000000^0000000000000+^^000000^N^B089^0000000015000+^0000000100000+^00000000^00000000^0000000000^00000000^000^000^0^0000^0000^19490827^00^00^0^L^002^001^^P02^N^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</inputFieldValue> <usrId xsi:type=\"soapenc:string\">fl_test</usrId> </multiRef> </soapenv:Body> </soapenv:Envelope>"
let postData = NSData(data: string.data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "https://www.etiqa.com.my/online-dsa/detaffws2/services/DETAFFServices?wsdl")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
*/
// let string = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><TransLinesByID xmlns=\"http://k3.ikea.org/WSTrans/\"><StoreNo>362</StoreNo><TransactionNo>950002174</TransactionNo></TransLinesByID></soap12:Body></soap12:Envelope>"
let string = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.holidaywebservice.com/HolidayService_v2/\"><SOAP-ENV:Body><ns1:GetHolidaysAvailable><ns1:countryCode>UnitedStates</ns1:countryCode></ns1:GetHolidaysAvailable></SOAP-ENV:Body></SOAP-ENV:Envelope>"
let postData = NSData(data: string.data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "http://www.holidaywebservice.com//HolidayService_v2/HolidayService2.asmx?wsdl")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print("error")
print(error!)
} else {
let httpResponse = response as? HTTPURLResponse
print("RESPONSE ISS")
print(httpResponse!)
let a = XMLParser(data: data as! Data)
a.delegate = self as XMLParserDelegate
a.parse()
for b in self.posts {
let ba = (b as AnyObject).object(forKey: "Code") as! String
self.resutlsArray.add(ba)
}
}
})
dataTask.resume()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
extension TestViewController : XMLParserDelegate {
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]){
element = elementName as NSString
if (elementName as NSString).isEqual(to: "Code"){
elements = NSMutableDictionary()
elements = [:]
title1 = NSMutableString()
title1 = ""
}
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
if element.isEqual(to: "Code"){
title1.append(string)
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?){
if (elementName as NSString).isEqual(to: "Code"){
if !title1.isEqual(nil){
elements.setObject(title1, forKey: "Code" as NSCopying)
}
posts.add(elements)
}
}
}
extension TestViewController : UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.resutlsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : Description2ViewCell = self.tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! Description2ViewCell
cell.descLabel.text = "self.resutlsArray[indexPath.row] as! String"
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment