public class XmlSlurper
extends DefaultHandler
Parse XML into a document tree that may be traversed similar to XPath expressions. For example:
import groovy.xml.XmlSlurper
def rootNode = new XmlSlurper().parseText(
'<root><one a1="uno!"/><two>Some text!</two></root>' )
assert rootNode.name() == 'root'
assert rootNode.one[0].@a1 == 'uno!'
assert rootNode.two.text() == 'Some text!'
rootNode.children().each { assert it.name() in ['one','two'] }
Note that in some cases, a 'selector' expression may not resolve to a single node. For example:
import groovy.xml.XmlSlurper
def rootNode = new XmlSlurper().parseText(
'''<root>
<a>one!</a>
<a>two!</a>
</root>''' )
assert rootNode.a.size() == 2
rootNode.a.each { assert it.text() in ['one!','two!'] }
A more realistic example — a book catalog. Given this XML:
<catalog>
<book id="b1">
<title>Programming Groovy 3</title>
<author>Venkat Subramaniam</author>
<year>2024</year>
</book>
<book id="b2">
<title>Groovy in Action</title>
<author>Dierk Koenig</author>
<year>2015</year>
</book>
</catalog>
the equivalent Groovy to slurp it and navigate the tree:
def catalog = new XmlSlurper().parseText(xml)
assert catalog.book.size() == 2
assert catalog.book[0].title.text() == 'Programming Groovy 3'
catalog.book.findAll { it.year.text().toInteger() >= 2020 }.each { book ->
println "${book.title} by ${book.author}"
}
| Constructor and description |
|---|
XmlSlurper()Creates a non-validating and namespace-aware XmlSlurper which does not allow DOCTYPE declarations in documents. |
XmlSlurper(boolean validating, boolean namespaceAware)Creates a XmlSlurper which does not allow DOCTYPE declarations in documents. |
XmlSlurper(boolean validating, boolean namespaceAware, boolean allowDocTypeDeclaration)Creates a XmlSlurper. |
XmlSlurper(XMLReader reader) |
XmlSlurper(SAXParser parser) |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public void |
characters(char[] ch, int start, int length) |
|
public void |
endDocument() |
|
public void |
endElement(String namespaceURI, String localName, String qName) |
|
public DTDHandler |
getDTDHandler() |
|
public GPathResult |
getDocument()
|
|
public EntityResolver |
getEntityResolver() |
|
public ErrorHandler |
getErrorHandler() |
|
public boolean |
getFeature(String uri) |
|
public Object |
getProperty(String uri) |
|
public void |
ignorableWhitespace(char[] buffer, int start, int len) |
|
public boolean |
isAllowDocTypeDeclaration()Determine if DOCTYPE declarations are allowed. |
|
public boolean |
isKeepIgnorableWhitespace()
|
|
public boolean |
isNamespaceAware()Determine if namespace handling is enabled. |
|
public boolean |
isValidating()Determine if the parser validates documents. |
|
public GPathResult |
parse(InputSource input)Parse the content of the specified input source into a GPathResult object |
|
public GPathResult |
parse(File file)Parses the content of the given file as XML turning it into a GPathResult object |
|
public GPathResult |
parse(InputStream input)Parse the content of the specified input stream into an GPathResult Object. |
|
public GPathResult |
parse(Reader in)Parse the content of the specified reader into a GPathResult Object. |
|
public GPathResult |
parse(String uri)Parse the content of the specified URI into a GPathResult Object |
|
public GPathResult |
parse(Path path)Parses the content of the file at the given path as XML turning it into a GPathResult object |
|
public GPathResult |
parseText(String text)A helper method to parse the given text as XML |
|
public void |
setAllowDocTypeDeclaration(boolean allowDocTypeDeclaration)Enable and/or disable DOCTYPE declaration support. |
|
public void |
setDTDHandler(DTDHandler dtdHandler) |
|
public void |
setEntityBaseUrl(URL base)Resolves entities against using the supplied URL as the base for relative URLs |
|
public void |
setEntityResolver(EntityResolver entityResolver) |
|
public void |
setErrorHandler(ErrorHandler errorHandler) |
|
public void |
setFeature(String uri, boolean value) |
|
public void |
setKeepIgnorableWhitespace(boolean keepIgnorableWhitespace)
|
|
public void |
setKeepWhitespace(boolean keepWhitespace)
|
|
public void |
setNamespaceAware(boolean namespaceAware)Enable and/or disable namespace handling. |
|
public void |
setProperty(String uri, Object value) |
|
public void |
setValidating(boolean validating)Enable and/or disable validation. |
|
public void |
startDocument() |
|
public void |
startElement(String namespaceURI, String localName, String qName, Attributes atts) |
|
public void |
startPrefixMapping(String tag, String uri) |
| Methods inherited from class | Name |
|---|---|
class DefaultHandler |
characters, declaration, endDocument, endElement, endPrefixMapping, equals, error, fatalError, getClass, hashCode, ignorableWhitespace, notationDecl, notify, notifyAll, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startElement, startPrefixMapping, toString, unparsedEntityDecl, wait, wait, wait, warning |
Creates a non-validating and namespace-aware XmlSlurper which does not allow DOCTYPE declarations in documents.
Parser options can be configured via setters before the first parse call:
// Using Groovy named parameters:
def slurper = new XmlSlurper(namespaceAware: false, keepIgnorableWhitespace: true)
Creates a XmlSlurper which does not allow DOCTYPE declarations in documents.
validating - true if the parser should validate documents as they are parsed; false otherwise.namespaceAware - true if the parser should provide support for XML namespaces; false otherwise. Creates a XmlSlurper.
validating - true if the parser should validate documents as they are parsed; false otherwise.namespaceAware - true if the parser should provide support for XML namespaces; false otherwise.allowDocTypeDeclaration - true if the parser should provide support for DOCTYPE declarations; false otherwise.
Determine if DOCTYPE declarations are allowed.
Determine if namespace handling is enabled.
Determine if the parser validates documents.
Parse the content of the specified input source into a GPathResult object
input - the InputSource to parseParses the content of the given file as XML turning it into a GPathResult object
file - the File to parseParse the content of the specified input stream into an GPathResult Object. Note that using this method will not provide the parser with any URI for which to find DTDs etc. It is up to you to close the InputStream after parsing is complete (if required).
input - the InputStream to parseParse the content of the specified reader into a GPathResult Object. Note that using this method will not provide the parser with any URI for which to find DTDs etc. It is up to you to close the Reader after parsing is complete (if required).
in - the Reader to parseParse the content of the specified URI into a GPathResult Object
uri - a String containing the URI to parseParses the content of the file at the given path as XML turning it into a GPathResult object
path - the path of the File to parseA helper method to parse the given text as XML
text - a String containing XML to parseEnable and/or disable DOCTYPE declaration support. Must be set before the first parse call.
allowDocTypeDeclaration - the new desired valueResolves entities against using the supplied URL as the base for relative URLs
base - The URL used to resolve relative URLs
keepIgnorableWhitespace - If true then ignorable whitespace (i.e. whitespace before elements) is kept.
The default is to discard the whitespace.keepWhitespace - If true then whitespace before elements is kept.
The default is to discard the whitespace.Enable and/or disable namespace handling. Must be set before the first parse call.
namespaceAware - the new desired valueEnable and/or disable validation. Must be set before the first parse call.
validating - the new desired valueCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.