Skip to content

Instantly share code, notes, and snippets.

@miceno
Created January 2, 2011 12:27
Show Gist options
  • Save miceno/762488 to your computer and use it in GitHub Desktop.
Save miceno/762488 to your computer and use it in GitHub Desktop.
Read XML and parse with Groovy Style
// Groovy
import static javax.xml.xpath.XPathConstants.*
import javax.xml.xpath.*
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
import org.w3c.dom.Document;
url= "http://stackoverflow.com/feeds/tag?tagnames=groovy&sort=newest"
xml= url.toURL().text
// Reading a Document Builder with Groovy Style
// Be aware!!!!: DOMBuilder should ignore namespaces
def doc= DOMBuilder.parse( new StringReader( xml), false, false).documentElement
println "debug document =" + doc.textContent
// Parsing with Groovy Xpath
use (DOMCategory)
{
nodes= doc.xpath( "//entry", NODESET)
nodes.each{ println "debug Element ${it.name}: ${it.textContent}" }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment