public class XmlSlurper
extends org.xml.sax.helpers.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()
Uses the defaults of not validating and namespace aware.
|
XmlSlurper(boolean validating,
boolean namespaceAware) |
XmlSlurper(javax.xml.parsers.SAXParser parser) |
XmlSlurper(org.xml.sax.XMLReader reader) |
Modifier and Type | Method and Description |
---|---|
void |
characters(char[] ch,
int start,
int length) |
void |
endDocument() |
void |
endElement(java.lang.String namespaceURI,
java.lang.String localName,
java.lang.String qName) |
GPathResult |
getDocument() |
org.xml.sax.DTDHandler |
getDTDHandler() |
org.xml.sax.EntityResolver |
getEntityResolver() |
org.xml.sax.ErrorHandler |
getErrorHandler() |
boolean |
getFeature(java.lang.String uri) |
java.lang.Object |
getProperty(java.lang.String uri) |
GPathResult |
parse(java.io.File file)
Parses the content of the given file as XML turning it into a GPathResult object
|
GPathResult |
parse(org.xml.sax.InputSource input)
Parse the content of the specified input source into a GPathResult object
|
GPathResult |
parse(java.io.InputStream input)
Parse the content of the specified input stream into an GPathResult Object.
|
GPathResult |
parse(java.io.Reader in)
Parse the content of the specified reader into a GPathResult Object.
|
GPathResult |
parse(java.lang.String uri)
Parse the content of the specified URI into a GPathResult Object
|
GPathResult |
parseText(java.lang.String text)
A helper method to parse the given text as XML
|
void |
setDTDHandler(org.xml.sax.DTDHandler dtdHandler) |
void |
setEntityBaseUrl(java.net.URL base)
Resolves entities against using the supplied URL as the base for relative URLs
|
void |
setEntityResolver(org.xml.sax.EntityResolver entityResolver) |
void |
setErrorHandler(org.xml.sax.ErrorHandler errorHandler) |
void |
setFeature(java.lang.String uri,
boolean value) |
void |
setKeepWhitespace(boolean keepWhitespace) |
void |
setProperty(java.lang.String uri,
java.lang.Object value) |
void |
startDocument() |
void |
startElement(java.lang.String namespaceURI,
java.lang.String localName,
java.lang.String qName,
org.xml.sax.Attributes atts) |
void |
startPrefixMapping(java.lang.String tag,
java.lang.String uri) |
public XmlSlurper() throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException
- if a parser cannot
be created which satisfies the requested configuration.org.xml.sax.SAXException
- for SAX errors.public XmlSlurper(boolean validating, boolean namespaceAware) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException
public XmlSlurper(org.xml.sax.XMLReader reader)
public XmlSlurper(javax.xml.parsers.SAXParser parser) throws org.xml.sax.SAXException
org.xml.sax.SAXException
public void setKeepWhitespace(boolean keepWhitespace)
keepWhitespace
- If true then whitespace before elements is kept.
The default is to discard the whitespace.public GPathResult getDocument()
public GPathResult parse(org.xml.sax.InputSource input) throws java.io.IOException, org.xml.sax.SAXException
input
- the InputSource to parseorg.xml.sax.SAXException
- Any SAX exception, possibly wrapping another exception.java.io.IOException
- An IO exception from the parser, possibly from a byte stream
or character stream supplied by the application.public GPathResult parse(java.io.File file) throws java.io.IOException, org.xml.sax.SAXException
file
- the File to parseorg.xml.sax.SAXException
- Any SAX exception, possibly wrapping another exception.java.io.IOException
- An IO exception from the parser, possibly from a byte stream
or character stream supplied by the application.public GPathResult parse(java.io.InputStream input) throws java.io.IOException, org.xml.sax.SAXException
input
- the InputStream to parseorg.xml.sax.SAXException
- Any SAX exception, possibly wrapping another exception.java.io.IOException
- An IO exception from the parser, possibly from a byte stream
or character stream supplied by the application.public GPathResult parse(java.io.Reader in) throws java.io.IOException, org.xml.sax.SAXException
in
- the Reader to parseorg.xml.sax.SAXException
- Any SAX exception, possibly wrapping another exception.java.io.IOException
- An IO exception from the parser, possibly from a byte stream
or character stream supplied by the application.public GPathResult parse(java.lang.String uri) throws java.io.IOException, org.xml.sax.SAXException
uri
- a String containing the URI to parseorg.xml.sax.SAXException
- Any SAX exception, possibly wrapping another exception.java.io.IOException
- An IO exception from the parser, possibly from a byte stream
or character stream supplied by the application.public GPathResult parseText(java.lang.String text) throws java.io.IOException, org.xml.sax.SAXException
text
- a String containing XML to parseorg.xml.sax.SAXException
- Any SAX exception, possibly wrapping another exception.java.io.IOException
- An IO exception from the parser, possibly from a byte stream
or character stream supplied by the application.public org.xml.sax.DTDHandler getDTDHandler()
public org.xml.sax.EntityResolver getEntityResolver()
public org.xml.sax.ErrorHandler getErrorHandler()
public boolean getFeature(java.lang.String uri) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException
public java.lang.Object getProperty(java.lang.String uri) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException
public void setDTDHandler(org.xml.sax.DTDHandler dtdHandler)
public void setEntityResolver(org.xml.sax.EntityResolver entityResolver)
public void setEntityBaseUrl(java.net.URL base)
base
- The URL used to resolve relative URLspublic void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
public void setFeature(java.lang.String uri, boolean value) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException
public void setProperty(java.lang.String uri, java.lang.Object value) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException
public void startDocument() throws org.xml.sax.SAXException
startDocument
in interface org.xml.sax.ContentHandler
startDocument
in class org.xml.sax.helpers.DefaultHandler
org.xml.sax.SAXException
public void startPrefixMapping(java.lang.String tag, java.lang.String uri) throws org.xml.sax.SAXException
startPrefixMapping
in interface org.xml.sax.ContentHandler
startPrefixMapping
in class org.xml.sax.helpers.DefaultHandler
org.xml.sax.SAXException
public void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts) throws org.xml.sax.SAXException
startElement
in interface org.xml.sax.ContentHandler
startElement
in class org.xml.sax.helpers.DefaultHandler
org.xml.sax.SAXException
public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException
characters
in interface org.xml.sax.ContentHandler
characters
in class org.xml.sax.helpers.DefaultHandler
org.xml.sax.SAXException
public void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName) throws org.xml.sax.SAXException
endElement
in interface org.xml.sax.ContentHandler
endElement
in class org.xml.sax.helpers.DefaultHandler
org.xml.sax.SAXException
public void endDocument() throws org.xml.sax.SAXException
endDocument
in interface org.xml.sax.ContentHandler
endDocument
in class org.xml.sax.helpers.DefaultHandler
org.xml.sax.SAXException