Created
January 2, 2011 11:35
-
-
Save miceno/762463 to your computer and use it in GitHub Desktop.
Reading XML with Java DocumentBuilder parsing with Java XPath
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: Reading XML with Java DocumentBuilder parsing with Java XPath | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.xpath.* | |
import org.w3c.dom.Document; | |
File f= new File( 'data.xml') | |
// Reading a Document Builder with Java style | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
Document doc = builder.parse(f); | |
println "document =" + doc.getDocumentElement().textContent | |
// Parsing | |
String XPATH_EXPRESSION="//entry" | |
// Parsing with Java Xpath | |
def xp = XPathFactory.newInstance().newXPath() | |
def expr = xp.compile(XPATH_EXPRESSION) | |
def nodes = expr.evaluate(doc, XPathConstants.NODESET) | |
nodes.each{ println "Element ${it.name}: ${it.textContent}" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment