BPM, Workflow, and Case

 View Only

Object to XML Conversion For SOAP Request Service

By Selcuk Colak posted Mon April 15, 2024 11:11 AM

  

This is a tw.object to SOAP request conversion service.

Service:

This service has five steps. You can find details about every steps.

Envelope Start:

Header:

Body:

Envelope End:

Set Output:

function escapeXmlMessage(message){
if (message != null && message != undefined) {
return String(message).replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
}
}
function formatDateStr(dateStr, format){
if(dateStr != null){
var date = new TWDate()
date.parse(dateStr, ("E MMM dd HH:mm:ss Z yyyy")); 
return date.format(format); }
else{ 
return ""; 
}
function getObjectFormat(objectName, objectsMap){
var returnObject = {};
if(objectsMap !== null && objectsMap !== undefined ){
for (var i=0; i<objectsMap.length; i++) {
if(objectName === objectsMap[i].objectName){
returnObject = objectsMap[i];
break;
}
}
}
return returnObject;
}
function getFormatedValue(value, objectName, objectsMap){
objectMap = getObjectFormat(objectName, objectsMap);
if(objectMap.objectName === null || objectMap.objectName === "" || objectMap.objectName === undefined){
if(value.constructor.name === "TWDate"){
return  formatDateStr(value, "yyyy-MM-dd");
}
return value;
}else{
if(objectMap.objectType === "Date"){
return  formatDateStr(value, objectMap.format);
}else{
return value;
}
}
}
//Returns child object namespace
function getChildObjectNameSpace(objectName,listOfObjects,defaultTarget){
if(listOfObjects != null){
for ( var idx = 0 ; idx < listOfObjects.length ; idx++ ) {
if(objectName == listOfObjects[idx].name){
if(listOfObjects[idx].value == undefined){
return defaultTarget;
}
return listOfObjects[idx].value;
}
}
return defaultTarget;
}else{
return defaultTarget;
}
}
//objectsMap can contain objectName(eg validDate), objectType(eg Date),format(eg YYYY-MM-dd)
function returnXml(object, xmlStr, objectsMap, lastPropertyName,bodyChildObjectNameSpace){
if(object != null){
try {
var length = object.propertyNames.length;
} catch (err) {
}
if(length != undefined){
for ( var idx = 0 ; idx < length ; idx++ ) {
var childNameSpace = getChildObjectNameSpace(object.propertyNames[idx],bodyChildObjectNameSpace,"body");
if(childNameSpace != null && childNameSpace != "" && childNameSpace != undefined){
childNameSpace = childNameSpace + ":"
}
xmlStr = xmlStr + "<"+childNameSpace+object.propertyNames[idx] +">";
if(typeof object.propertyValues[idx] == "object"){
xmlStr = returnXml(object.propertyValues[idx], xmlStr, objectsMap, object.propertyNames[idx],bodyChildObjectNameSpace);
}else{
xmlStr = xmlStr + escapeXmlMessage(getFormatedValue(object.propertyValues[idx], object.propertyNames[idx], objectsMap));
}
xmlStr = xmlStr + "</"+childNameSpace+ object.propertyNames[idx] +">";
}
}else{
//This means it is list object
if(object.constructor.name == "TWObject"){
for (var i=0; i<object.length; i++) {
xmlStr = returnXml(object[i], xmlStr, objectsMap, "",bodyChildObjectNameSpace);
if(i < (object.length - 1) && lastPropertyName != "") {
var childNameSpace = getChildObjectNameSpace(lastPropertyName,bodyChildObjectNameSpace,"body");
if(childNameSpace != null && childNameSpace != "" && childNameSpace != undefined){
childNameSpace = childNameSpace + ":"
}
xmlStr = xmlStr + "</"+childNameSpace+ lastPropertyName +">";
xmlStr = xmlStr + "<"+childNameSpace+ lastPropertyName +">";
}
}else{
xmlStr = xmlStr + escapeXmlMessage(getFormatedValue(object, lastPropertyName, objectsMap));
}
}
}
return xmlStr;
}
tw.local.bodyXml = "";
if(tw.local.body != null){
var xmlStr = "";
tw.local.bodyXml = "<"+tw.local.soapName+":Body>";
tw.local.bodyXml = tw.local.bodyXml + "<body:"+tw.local.bodyObjectName+" xmlns:body=\""+tw.local.bodyNameSpace+"\">";
tw.local.bodyXml = tw.local.bodyXml + returnXml(tw.local.body,xmlStr,tw.local.objectTypes,"",tw.local.bodyChildObjectNameSpace);
tw.local.bodyXml = tw.local.bodyXml + "</body:" + tw.local.bodyObjectName + ">";
tw.local.bodyXml = tw.local.bodyXml + "</"+tw.local.soapName+":Body>";

Inputs:

header: is the header object which we are going to create header for SOAP request

body: is the body object which we are going to create body for SOAP request

header Namespace: is the namespace of header

bodyNameSpace: is the namespace of body

bodyObjectName: is the object name of the body

headerObjectName: is the object name of the header

objectType: is the special formats for the object. For example if we have two date fields in our object and if we want to set date format individually, we use objectType name value pair to differentiate object formats.

envelopeNameSpace: is the Envelope name space

soapName: is the name of the Soap

bodyChildObjectNameSpace: is the variable we use in case of we need to set different namespaces for different tags. If we add this parameter like below we see the attribute like below.

New: <subbodytag:variable1>Body1</body:subbodytag>

Old: <body:variable1>Body1</body:variable1>

tw.local.bodyChildObjectNameSpace = new tw.object.listOf.NameValuePair();
tw.local.bodyChildObjectNameSpace[0] = new tw.object.NameValuePair();

tw.local.bodyChildObjectNameSpace[0].name = "variable1";
tw.local.bodyChildObjectNameSpace[0].value = "subbodytag";

Example:

Creating the objects for example:

Converting object to XML

Output:

<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SOAP-ENV:Header>
        <header:HeaderObject
            xmlns:header="http://www.w3.org/TR/Header">
            <header:variable1>Header1</header:variable1>
            <header:variable2>Header2</header:variable2>
            <header:variable3>Header3</header:variable3>
        </header:HeaderObject>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <body:BodyObject
            xmlns:body="http://www.w3.org/TR/Body">
            <body:variable1>Body1</body:variable1>
            <body:variable2>Body2</body:variable2>
            <body:variable3>Body3</body:variable3>
        </body:BodyObject>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

0 comments
3 views

Permalink