Since I found this such a huge pain in the arse I decided to commit it to a blog for posterity. May someone find this someday and save themselves a few hours of frustration. Note that this code works for retrieving an element of type "integer" but could be altered to cast other base types, such as "string", "boolean" etc.