Parse XML Data and Generate JSON Message with API Call
import com.apinizer.common.util.*
try{
// clear table
def get = new URL("http://192.168.100.100:30080/apigateway/employee/clean").openConnection();
get.setRequestMethod("GET")
get.setDoOutput(true)
get.setRequestProperty("Content-Type", "application/json")
get.setRequestProperty("Username", "")
get.setRequestProperty("Password", "")
def getRC = get.getResponseCode();
if(getRC.equals(400)|| getRC.equals(401)) {
responseErrorMessageToClient="Authorization Error!";
}
}catch(Exception e){
}
int itemCount = responseBodyTextToClient.count("<item>");
String templateJson = "{"+
"\"registration_no\":\"#registration_no\", "+
"\"name\" :\"#name\","+
"\"surname\" :\"#surname\","+
"\"workplace_code\" :\"#workplace_code\","+
"\"workplace\" :\"#workplace\","+
"\"hr_info\" :\"#hr_info\","+
"\"department\" :\"#department\","+
"\"title\" :\"#title\","+
"\"duty\" :\"duty\","+
"\"birth_day\" :\"#birth_day\","+
"\"start_date_of_work\" :\"#start_date_of_work\","+
"\"manager_name\" :\"#manager_name\","+
"\"manager_surname\" :\"#manager_surname\","+
"\"manager_registration\" :\"#manager_registration\","+
"\"email\" :\"#email\","+
"\"phone\" :\"#phone\","+
"\"identity_no\":\"#identity_no\""+
"}"
;
String wrongIdStr="";
for(int i=1; i < itemCount+1; i++){
String valueJson = templateJson;
String xpathId = "(/ArrayList/item)["+i+"]/id";
String valueId = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpathId, true);
try{
String xpath = "(/ArrayList/item)["+i+"]/secondaryNr";
String value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#registration_no" , value );
xpath = "(/ArrayList/item)["+i+"]/name";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#name" , value);
xpath = "(/ArrayList/item)["+i+"]/surname";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#surname" , value);
xpath = "(/ArrayList/item)["+i+"]/bizUnit/code";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#workplace_code" , value);
xpath = "(/ArrayList/item)["+i+"]/bizUnit/description";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#workplace" , value);
xpath = "(/ArrayList/item)["+i+"]/orgUnit/code";
value = UtilXmlOperations.extractFromXmlBody(responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#hs_info" , value);
xpath = "(/ArrayList/item)["+i+"]/orgUnit/description";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#department" , value);
xpath = "(/ArrayList/item)["+i+"]/profession";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#title" , value);
xpath = "(/ArrayList/item)["+i+"]/jobTitle";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#duty" , value);
xpath = "(/ArrayList/item)["+i+"]/birthday";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
if(org.apache.commons.lang3.StringUtils.isNotBlank(value) && value.length()>10){
value=value.substring(0,10);
}
valueJson = valueJson.replace("#birth_day" , value);
xpath = "(/ArrayList/item)["+i+"]/entryDate";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
if(org.apache.commons.lang3.StringUtils.isNotBlank(value) && value.length()>10){
value=value.substring(0,10);
}
valueJson = valueJson.replace("#start_date_of_work" , value);
xpath = "(/ArrayList/item)["+i+"]/supervisor/name";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#manager_name" , value);
xpath = "(/ArrayList/item)["+i+"]/supervisor/surname";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#manager_surname" , value);
xpath = "(/ArrayList/item)["+i+"]/supervisor/secondaryNr";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#manager_registration" , value);
xpath = "(/ArrayList/item)["+i+"]/contactInfo/email";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#email" , value);
xpath = "(/ArrayList/item)["+i+"]/contactInfo/mobile1";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#phone" , value);
xpath = "(/ArrayList/item)["+i+"]/officialId";
value = UtilXmlOperations.extractFromXmlBody( responseBodyTextFromTargetAPI, xpath, true);
valueJson = valueJson.replace("#identity_no" , value);
// POST to DB
def post = new URL("http://192.168.100.100:30080/apigateway/employee/save").openConnection();
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/json")
post.setRequestProperty("Username", "")
post.setRequestProperty("Password", "")
post.getOutputStream().write(valueJson.getBytes("UTF-8"));
def postRC = post.getResponseCode();
if(postRC.equals(400)|| postRC.equals(401)) {
responseErrorMessageToClient="Authorization Error !";
break;
}
if(!postRC.equals(200)) {
wrongIdStr=valueId+","+wrongiIdStr;
}
}catch(Exception e){
wrongIdStr=valueId+","+wrongIdStr;
}
}
if(wrongIdStr.endsWith(",")){
wrongIdStr = wrongIdStr.substring(0,wrongIdStr.length() - 1);
}
if(org.apache.commons.lang3.StringUtils.isNotBlank(wrongIdStr)){
responseErrorMessageToClient=wrongIdStr+" records with it could not be saved!";
}else{
responseBodyTextToClient=
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"+
"<soap:Body>"+
" <result>OK</result>"+
"</soap:Body>"+
"</soap:Envelope>";
}
GROOVY