Skip to content

Instantly share code, notes, and snippets.

@folkengine
Created January 4, 2012 16:45
Show Gist options
  • Save folkengine/1560888 to your computer and use it in GitHub Desktop.
Save folkengine/1560888 to your computer and use it in GitHub Desktop.
Grails XML Parsing By ID
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
class XMLExample implements ApplicationContextAware {
def applicationContext
/**
* <?xml version="1.0" encoding="UTF-8"?>
* <elementNames>
* <elementName id="UniqueIDInXMLFile" bar="baz" />
* <elementName id="AnotherUniqueIDInXMLFile" bar="baz" />
* </elementNames>
*
* @return
*/
def getXMLElement() {
def myfile = applicationContext.getResource("/WEB-INF/xml/file.xml").getFile()
// groovy.util.slurpersupport.GPathResult
def res = XML.parse(myfile.text)
def myElement = res.elementName.find { it.@id == "UniqueIDInXMLFile" }
def foo
foo.bar = myElement.@bar.text()
if (myElement.@bool.text() == "true") {
foo.bool = true
} else {
foo.bool = false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment