Created
January 2, 2011 12:27
-
-
Save miceno/762488 to your computer and use it in GitHub Desktop.
Read XML and parse with Groovy Style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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