Skip to content

Instantly share code, notes, and snippets.

@bmakarand2009
Created November 15, 2013 02:52
Show Gist options
  • Save bmakarand2009/7478354 to your computer and use it in GitHub Desktop.
Save bmakarand2009/7478354 to your computer and use it in GitHub Desktop.
parse xml file
private def parseXmlClosure = { pRoot,parentObj,user,curId ->
def xmlMap=[:]
def rootName = pRoot.name()
def domainClz = SingUtils.DOMAIN_OBJECTS_MAP."${rootName}"
log.debug "domainc lass loaded as ${domainClz}"
def curObj=null
if(curId) {
curObj = domainClz.get(curId)
}
def xmlElementsMap = domainClz.XML_ELEMENT_MAP
xmlElementsMap.each{k,v->
def childElem = pRoot."$k"[0]
if(childElem){
String childElemName = childElem.name()
log.debug "going throught childelements of list $childElemName"
def childElemVal = childElem.value()[0]
String aType = childElem.attribute("type")
if(!aType){
if(childElemName.endsWith("_list")){
def xmlListObj=[]
childElem.children().each{ it ->
Integer listObjId= null
String listObjIdStr = it.id.text()
if(listObjIdStr) listObjId = listObjIdStr.toInteger()
xmlListObj<<parseXmlClosure(it,curObj,user,listObjId)
}
xmlMap."$v" = xmlListObj
}else
xmlMap."$v" = childElemVal
}
else if(aType==SingUtils.INTEGER_TYPE) xmlMap."$v" = childElemVal.toInteger()
else if(aType==SingUtils.DATETIME_TYPE)xmlMap."$v" = new Date().parse("yyyy-M-d H:m:s",childElemVal.toString())
}
}
def aObj=null
if(xmlMap)
aObj = domainClz.createOrUpdate(rootName,domainClz,xmlMap,curObj,parentObj,user)
else
log.debug "No xmlMap properties found,Make sure XML_ELEMENT_MAP of $domainClz has the correct Mappings "
return aObj
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment