Skip to content

Instantly share code, notes, and snippets.

@robvalk
Created December 2, 2013 09:50
Show Gist options
  • Save robvalk/7747266 to your computer and use it in GitHub Desktop.
Save robvalk/7747266 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