Skip to content

Instantly share code, notes, and snippets.

@stevengonsalvez
Forked from robvalk/xml2map.groovy
Created December 16, 2015 23:49
Show Gist options
  • Save stevengonsalvez/b087e9d554c1af83111d to your computer and use it in GitHub Desktop.
Save stevengonsalvez/b087e9d554c1af83111d to your computer and use it in GitHub Desktop.
Groovy data mapping from XML to Java Lists/Maps
// no need to use builder here, just raw Groovy map syntax
//get DOMCategory to get access to GPATH style operations like getNode getNodeAt etc.
import au.com.sixtree.java.esb.mapper.DOMCategory;
//declare a java.util.Map to hold results
def result = [:]
//get input root element
def _in = payload.documentElement
use(DOMCategory) {
/*using DOMCategory get access to various xml elements, this is like calling
*_in.bookId.text() is equivalent to responseDoc.getDocumentElement().getElementsByTagName("book").item(0).getFirstChild().getTextContent());
*result.bookId is equivalent to result.put("bookId" , "123456") in Java
*/
result.bookId = _in.bookId.text()
//Using ?: Java operator to create an if else condition
/*
* Set results("inventoryStatus") value only if the inbound element has a non-empty value
* One of the setter calls a custom XREF method (ValueCrossReferenceHelper.lookup(String args...)) to retrieve the corresponding system value
* */
(_in.inventoryStatus.text() != null && _in.inventoryStatus.text()!="")?(result.inventoryStatus = au.com.sixtree.esb.common.crossreferenceclient.ValueCrossReferenceHelper.lookup("Book Inventory Status", "Outbound_System_Name", _in.inventoryStatus.text() , "Inbound_System_Name")): (result.inventoryStatus = "");
}
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment