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!'] }
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 |
isKeepIgnorableWhitespace()
|
|
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) |
|
public GPathResult |
parseText(String text) A helper method to parse the given text as XML |
|
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 |
setProperty(String uri, Object value) |
|
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 |
fatalError, setDocumentLocator, startDocument, endDocument, startPrefixMapping, endPrefixMapping, startElement, endElement, characters, ignorableWhitespace, processingInstruction, skippedEntity, unparsedEntityDecl, notationDecl, resolveEntity, error, warning, wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll, declaration |
Creates a non-validating and namespace-aware XmlSlurper
which does not allow DOCTYPE declarations in documents.
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.
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 parseA helper method to parse the given text as XML
text
- a String containing XML to parseResolves 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.Copyright © 2003-2024 The Apache Software Foundation. All rights reserved.