public class XmlSlurper extends DefaultHandler
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:
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!'] }
GPathResult| Constructor and Description |
|---|
XmlSlurper()
Creates a non-validating and non-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(SAXParser parser) |
XmlSlurper(XMLReader reader) |
| Modifier and Type | Method and Description |
|---|---|
void |
characters(char[] ch,
int start,
int length) |
void |
endDocument() |
void |
endElement(String namespaceURI,
String localName,
String qName) |
GPathResult |
getDocument() |
DTDHandler |
getDTDHandler() |
EntityResolver |
getEntityResolver() |
ErrorHandler |
getErrorHandler() |
boolean |
getFeature(String uri) |
Object |
getProperty(String uri) |
void |
ignorableWhitespace(char[] buffer,
int start,
int len) |
boolean |
isKeepIgnorableWhitespace() |
GPathResult |
parse(File file)
Parses the content of the given file as XML turning it into a GPathResult object
|
GPathResult |
parse(InputSource input)
Parse the content of the specified input source into a GPathResult object
|
GPathResult |
parse(InputStream input)
Parse the content of the specified input stream into an GPathResult Object.
|
GPathResult |
parse(Reader in)
Parse the content of the specified reader into a GPathResult Object.
|
GPathResult |
parse(String uri)
Parse the content of the specified URI into a GPathResult Object
|
GPathResult |
parseText(String text)
A helper method to parse the given text as XML
|
void |
setDTDHandler(DTDHandler dtdHandler) |
void |
setEntityBaseUrl(URL base)
Resolves entities against using the supplied URL as the base for relative URLs
|
void |
setEntityResolver(EntityResolver entityResolver) |
void |
setErrorHandler(ErrorHandler errorHandler) |
void |
setFeature(String uri,
boolean value) |
void |
setKeepIgnorableWhitespace(boolean keepIgnorableWhitespace) |
void |
setKeepWhitespace(boolean keepWhitespace)
Deprecated.
use setKeepIgnorableWhitespace
|
void |
setProperty(String uri,
Object value) |
void |
startDocument() |
void |
startElement(String namespaceURI,
String localName,
String qName,
Attributes atts) |
void |
startPrefixMapping(String tag,
String uri) |
endPrefixMapping, error, fatalError, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, unparsedEntityDecl, warningpublic XmlSlurper()
throws ParserConfigurationException,
SAXException
XmlSlurper which does not allow DOCTYPE declarations in documents.ParserConfigurationException - if no parser which satisfies the requested configuration can be created.SAXException - for SAX errors.public XmlSlurper(boolean validating,
boolean namespaceAware)
throws ParserConfigurationException,
SAXException
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.ParserConfigurationException - if no parser which satisfies the requested configuration can be created.SAXException - for SAX errors.public XmlSlurper(boolean validating,
boolean namespaceAware,
boolean allowDocTypeDeclaration)
throws ParserConfigurationException,
SAXException
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.ParserConfigurationException - if no parser which satisfies the requested configuration can be created.SAXException - for SAX errors.public XmlSlurper(XMLReader reader)
public XmlSlurper(SAXParser parser) throws SAXException
SAXException@Deprecated public void setKeepWhitespace(boolean keepWhitespace)
keepWhitespace - If true then whitespace before elements is kept.
The default is to discard the whitespace.public void setKeepIgnorableWhitespace(boolean keepIgnorableWhitespace)
keepIgnorableWhitespace - If true then ignorable whitespace (i.e. whitespace before elements) is kept.
The default is to discard the whitespace.public boolean isKeepIgnorableWhitespace()
public GPathResult getDocument()
public GPathResult parse(InputSource input) throws IOException, SAXException
input - the InputSource to parseSAXException - 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.public GPathResult parse(File file) throws IOException, SAXException
file - the File to parseSAXException - 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.public GPathResult parse(InputStream input) throws IOException, SAXException
input - the InputStream to parseSAXException - 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.public GPathResult parse(Reader in) throws IOException, SAXException
in - the Reader to parseSAXException - 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.public GPathResult parse(String uri) throws IOException, SAXException
uri - a String containing the URI to parseSAXException - 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.public GPathResult parseText(String text) throws IOException, SAXException
text - a String containing XML to parseSAXException - 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.public DTDHandler getDTDHandler()
public EntityResolver getEntityResolver()
public ErrorHandler getErrorHandler()
public boolean getFeature(String uri) throws SAXNotRecognizedException, SAXNotSupportedException
public Object getProperty(String uri) throws SAXNotRecognizedException, SAXNotSupportedException
public void setDTDHandler(DTDHandler dtdHandler)
public void setEntityResolver(EntityResolver entityResolver)
public void setEntityBaseUrl(URL base)
base - The URL used to resolve relative URLspublic void setErrorHandler(ErrorHandler errorHandler)
public void setFeature(String uri, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException
public void setProperty(String uri, Object value) throws SAXNotRecognizedException, SAXNotSupportedException
public void startDocument()
throws SAXException
startDocument in interface ContentHandlerstartDocument in class DefaultHandlerSAXExceptionpublic void startPrefixMapping(String tag, String uri) throws SAXException
startPrefixMapping in interface ContentHandlerstartPrefixMapping in class DefaultHandlerSAXExceptionpublic void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
startElement in interface ContentHandlerstartElement in class DefaultHandlerSAXExceptionpublic void ignorableWhitespace(char[] buffer,
int start,
int len)
throws SAXException
ignorableWhitespace in interface ContentHandlerignorableWhitespace in class DefaultHandlerSAXExceptionpublic void characters(char[] ch,
int start,
int length)
throws SAXException
characters in interface ContentHandlercharacters in class DefaultHandlerSAXExceptionpublic void endElement(String namespaceURI, String localName, String qName) throws SAXException
endElement in interface ContentHandlerendElement in class DefaultHandlerSAXExceptionpublic void endDocument()
throws SAXException
endDocument in interface ContentHandlerendDocument in class DefaultHandlerSAXException