Package groovy.xml
Class XmlSlurper
java.lang.Object
org.xml.sax.helpers.DefaultHandler
groovy.xml.XmlSlurper
- All Implemented Interfaces:
ContentHandler,DTDHandler,EntityResolver,ErrorHandler
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!'] }
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a non-validating and namespace-awareXmlSlurperwhich does not allow DOCTYPE declarations in documents.XmlSlurper(boolean validating, boolean namespaceAware) Creates aXmlSlurperwhich does not allow DOCTYPE declarations in documents.XmlSlurper(boolean validating, boolean namespaceAware, boolean allowDocTypeDeclaration) Creates aXmlSlurper.XmlSlurper(SAXParser parser) XmlSlurper(XMLReader reader) -
Method Summary
Modifier and TypeMethodDescriptionvoidcharacters(char[] ch, int start, int length) voidvoidendElement(String namespaceURI, String localName, String qName) booleangetFeature(String uri) getProperty(String uri) voidignorableWhitespace(char[] buffer, int start, int len) booleanParses the content of the given file as XML turning it into a GPathResult objectparse(InputStream input) Parse the content of the specified input stream into an GPathResult Object.Parse the content of the specified reader into a GPathResult Object.Parse the content of the specified URI into a GPathResult Objectparse(InputSource input) Parse the content of the specified input source into a GPathResult objectA helper method to parse the given text as XMLvoidsetDTDHandler(DTDHandler dtdHandler) voidsetEntityBaseUrl(URL base) Resolves entities against using the supplied URL as the base for relative URLsvoidsetEntityResolver(EntityResolver entityResolver) voidsetErrorHandler(ErrorHandler errorHandler) voidsetFeature(String uri, boolean value) voidsetKeepIgnorableWhitespace(boolean keepIgnorableWhitespace) voidsetKeepWhitespace(boolean keepWhitespace) Deprecated.use setKeepIgnorableWhitespacevoidsetProperty(String uri, Object value) voidvoidstartElement(String namespaceURI, String localName, String qName, Attributes atts) voidstartPrefixMapping(String tag, String uri) Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, error, fatalError, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, unparsedEntityDecl, warningMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.xml.sax.ContentHandler
declaration
-
Constructor Details
-
XmlSlurper
Creates a non-validating and namespace-awareXmlSlurperwhich does not allow DOCTYPE declarations in documents.- Throws:
ParserConfigurationException- if no parser which satisfies the requested configuration can be created.SAXException- for SAX errors.
-
XmlSlurper
public XmlSlurper(boolean validating, boolean namespaceAware) throws ParserConfigurationException, SAXException Creates aXmlSlurperwhich does not allow DOCTYPE declarations in documents.- Parameters:
validating-trueif the parser should validate documents as they are parsed; false otherwise.namespaceAware-trueif the parser should provide support for XML namespaces;falseotherwise.- Throws:
ParserConfigurationException- if no parser which satisfies the requested configuration can be created.SAXException- for SAX errors.
-
XmlSlurper
public XmlSlurper(boolean validating, boolean namespaceAware, boolean allowDocTypeDeclaration) throws ParserConfigurationException, SAXException Creates aXmlSlurper.- Parameters:
validating-trueif the parser should validate documents as they are parsed; false otherwise.namespaceAware-trueif the parser should provide support for XML namespaces;falseotherwise.allowDocTypeDeclaration-trueif the parser should provide support for DOCTYPE declarations;falseotherwise.- Throws:
ParserConfigurationException- if no parser which satisfies the requested configuration can be created.SAXException- for SAX errors.
-
XmlSlurper
-
XmlSlurper
- Throws:
SAXException
-
-
Method Details
-
setKeepWhitespace
Deprecated.use setKeepIgnorableWhitespace- Parameters:
keepWhitespace- If true then whitespace before elements is kept. The default is to discard the whitespace.
-
setKeepIgnorableWhitespace
public void setKeepIgnorableWhitespace(boolean keepIgnorableWhitespace) - Parameters:
keepIgnorableWhitespace- If true then ignorable whitespace (i.e. whitespace before elements) is kept. The default is to discard the whitespace.
-
isKeepIgnorableWhitespace
public boolean isKeepIgnorableWhitespace()- Returns:
- true if ignorable whitespace is kept
-
getDocument
- Returns:
- The GPathResult instance created by consuming a stream of SAX events Note if one of the parse methods has been called then this returns null Note if this is called more than once all calls after the first will return null
-
parse
Parse the content of the specified input source into a GPathResult object- Parameters:
input- the InputSource to parse- Returns:
- An object which supports GPath expressions
- Throws:
SAXException- Any SAX exception, possibly wrapping another exception.IOException- An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
-
parse
Parses the content of the given file as XML turning it into a GPathResult object- Parameters:
file- the File to parse- Returns:
- An object which supports GPath expressions
- Throws:
SAXException- Any SAX exception, possibly wrapping another exception.IOException- An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
-
parse
Parse 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).- Parameters:
input- the InputStream to parse- Returns:
- An object which supports GPath expressions
- Throws:
SAXException- Any SAX exception, possibly wrapping another exception.IOException- An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
-
parse
Parse 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).- Parameters:
in- the Reader to parse- Returns:
- An object which supports GPath expressions
- Throws:
SAXException- Any SAX exception, possibly wrapping another exception.IOException- An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
-
parse
Parse the content of the specified URI into a GPathResult Object- Parameters:
uri- a String containing the URI to parse- Returns:
- An object which supports GPath expressions
- Throws:
SAXException- Any SAX exception, possibly wrapping another exception.IOException- An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
-
parse
- Throws:
IOExceptionSAXException
-
parseText
A helper method to parse the given text as XML- Parameters:
text- a String containing XML to parse- Returns:
- An object which supports GPath expressions
- Throws:
SAXException- Any SAX exception, possibly wrapping another exception.IOException- An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
-
getDTDHandler
-
getEntityResolver
-
getErrorHandler
-
getFeature
-
getProperty
-
setDTDHandler
-
setEntityResolver
-
setEntityBaseUrl
Resolves entities against using the supplied URL as the base for relative URLs- Parameters:
base- The URL used to resolve relative URLs
-
setErrorHandler
-
setFeature
public void setFeature(String uri, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException -
setProperty
public void setProperty(String uri, Object value) throws SAXNotRecognizedException, SAXNotSupportedException -
startDocument
- Specified by:
startDocumentin interfaceContentHandler- Overrides:
startDocumentin classDefaultHandler- Throws:
SAXException
-
startPrefixMapping
- Specified by:
startPrefixMappingin interfaceContentHandler- Overrides:
startPrefixMappingin classDefaultHandler- Throws:
SAXException
-
startElement
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException - Specified by:
startElementin interfaceContentHandler- Overrides:
startElementin classDefaultHandler- Throws:
SAXException
-
ignorableWhitespace
- Specified by:
ignorableWhitespacein interfaceContentHandler- Overrides:
ignorableWhitespacein classDefaultHandler- Throws:
SAXException
-
characters
- Specified by:
charactersin interfaceContentHandler- Overrides:
charactersin classDefaultHandler- Throws:
SAXException
-
endElement
- Specified by:
endElementin interfaceContentHandler- Overrides:
endElementin classDefaultHandler- Throws:
SAXException
-
endDocument
- Specified by:
endDocumentin interfaceContentHandler- Overrides:
endDocumentin classDefaultHandler- Throws:
SAXException
-