Background
The purpose of this article is to show you how to use the PBDOM object to build the specified XML content for the parameter of an HTTP request sent with the HTTPClient object and how to obtain the specified content in the returned XML string.
Implementation Steps
1. Load the PBDOM Object in the PB Application
You can add pbdom170.pbd directly into your application (Default Path: C:\Program Files (x86)\Appeon\Shared\PowerBuilder) or import the PBDOM170.pbx file into a PBL in your application.
2. Use the PBDOM Object to Build the Specified XML String
Open the SOAP Web service on IE as shown below. Copy the associated SOAP sample request. Refer to the red content below. (If you don’t see this content, please contact your service provider to get such a sample request.)
The content of the XML string to be built:
<?xml version="1.0" encoding="utf-8"?> <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> <of_add xmlns="http://tempurl.org"> <ai_test>short</ai_test> </of_add> </soap:Body> </soap:Envelope>
If you want to simply use a string to build the XML content and send it through HTTPClient, the code is as follows:
HttpClient lhc_client Integer li_ret , i , li_StatusCode String ls_url , ls_data, ls_body , ls_ret ls_url ="http://localhost/invoice/n_webservice.asmx" ls_body = '<?xml version="1.0" encoding="utf-8"?>'+& '<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>'+& ' <of_add xmlns="http://tempurl.org">'+& ' <ai_test>2</ai_test>'+& ' </of_add>'+& ' </soap:Body>'+& '</soap:Envelope>' lhc_client = CREATE httpClient lhc_client.SetRequestHeader("Content-Type", "text/xml") lhc_client.sendrequest('POST',ls_url,ls_body) li_StatusCode = lhc_client.GetResponseStatusCode() ls_ret = lhc_client.GetResponseStatusText( ) li_ret = lhc_client.getresponsebody(ls_data) DESTROY lhc_client
If you use the PBDOM object to build the XML content, here's what it looks like:
HttpClient lhc_client PBDOM_Builder lpbdom_Builder PBDOM_Document lpbdom_doc PBDOM_ProcessingInstruction lpbdom_process PBDOM_ELEMENT lpbdom_root, lpbdom_body, lpbdom_method, lpbdom_arg Integer li_ret , i , li_StatusCode String ls_url, ls_data, ls_value, ls_body, ls_ret, ls_rootname ls_url ="http://localhost/invoice/n_webservice.asmx" ls_rootname ="Envelope" TRY lpbdom_doc = CREATE PBDOM_Document lpbdom_doc.newdocument(ls_rootname) lpbdom_process = CREATE PBDOM_ProcessingInstruction lpbdom_process.setname("xml") lpbdom_process.setvalue("version", "1.0") lpbdom_process.setvalue("encoding", "utf-8") lpbdom_doc.addcontent(lpbdom_process) DESTROY lpbdom_process lpbdom_root = lpbdom_doc.getrootelement() lpbdom_root.AddNameSpaceDeclaration('xsd',"http://www.w3.org/2001/XMLSchema") lpbdom_root.SetNameSpace('xsd',"http://www.w3.org/2001/XMLSchema",False) lpbdom_root.AddNameSpaceDeclaration('xsi',"http://www.w3.org/2001/XMLSchema-instance") lpbdom_root.SetNameSpace('xsi',"http://www.w3.org/2001/XMLSchema-instance",False) lpbdom_root.AddNameSpaceDeclaration('soap',"http://schemas.xmlsoap.org/soap/envelope/") lpbdom_root.SetNameSpace('soap',"http://schemas.xmlsoap.org/soap/envelope/",False) //body lpbdom_body = CREATE PBDOM_Element lpbdom_body.setname( "Body") lpbdom_body.setnamespace( "soap", "http://schemas.xmlsoap.org/soap/envelope/", False) //of_add lpbdom_method = CREATE PBDOM_Element lpbdom_method.setname( "of_add") lpbdom_method.addnamespacedeclaration( "", "http://tempurl.org") //ai_test lpbdom_arg = CREATE PBDOM_Element lpbdom_arg.setname( "ai_test") lpbdom_arg.settext( "2") //addcontent lpbdom_method.addcontent( lpbdom_arg) lpbdom_body.addcontent( lpbdom_method) lpbdom_root.addcontent( lpbdom_body) ls_body = lpbdom_doc.savedocumentintostring( ) //get new xml CATCH ( PBDOM_Exception pbde ) MessageBox( "PBDOM Exception", pbde.getMessage() ) CATCH ( PBXRuntimeError re ) MessageBox( "PBNI Exception", re.getMessage() ) END TRY IF isvalid(lpbdom_arg) THEN DESTROY lpbdom_arg IF isvalid(lpbdom_method) THEN DESTROY lpbdom_method IF isvalid(lpbdom_body) THEN DESTROY lpbdom_body IF isvalid(lpbdom_doc) THEN DESTROY lpbdom_doc lhc_client = CREATE httpClient lhc_client.SetRequestHeader("Content-Type", "text/xml") lhc_client.sendrequest('POST',ls_url,ls_body) li_StatusCode = lhc_client.GetResponseStatusCode() ls_ret = lhc_client.GetResponseStatusText( ) li_ret = lhc_client.getresponsebody( ls_data) DESTROY lhc_client
3. Use the PBDOM Object to Get the Specified Content in the XML String.
The content of the XML string to be obtained:
<?xml version="1.0" encoding="utf-8"?> <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> <of_addResponse xmlns="http://tempurl.org"> <of_addResult>short</of_addResult> </of_addResponse> </soap:Body> </soap:Envelope>
Execute the following PB code can get the value of the of_addResult element in the returned XML string from the Soap Web Service.
lpbdom_Builder = CREATE PBDOM_BUILDER TRY // generate XML Document lpbdom_Doc = lpbdom_Builder.BuildFromString( ls_data ) ls_value = lpbdom_Doc.GetRootElement().& GetChildElement("Body", "soap","http://schemas.xmlsoap.org/soap/envelope/").& GetChildElement("of_addResponse","","http://tempurl.org").& GetChildElement("of_addResult","","http://tempurl.org").gettext() CATCH (PBDOM_Exception lpbdom_Except) // Error Handling goes here, MessageBox( "PBDOM_Exception", lpbdom_Except.GetExceptionCode()) END TRY DESTROY lpbdom_Builder