Groovy Documentation

groovy.util
[Java] Class XmlSlurper

java.lang.Object
  org.xml.sax.helpers.DefaultHandler
      groovy.util.XmlSlurper

public class XmlSlurper
extends org.xml.sax.helpers.DefaultHandler

Parse XML into a document tree that may be traversed similar to XPath expressions. For example:

 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!'] }
 
Authors:
John Wilson
See Also:
GPathResult


Constructor Summary
XmlSlurper()

Uses the defaults of not validating and namespace aware.

XmlSlurper(boolean validating, boolean namespaceAware)

XmlSlurper(org.xml.sax.XMLReader reader)

XmlSlurper(javax.xml.parsers.SAXParser parser)

 
Method Summary
void characters(char[] ch, int start, int length)

void endDocument()

void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)

org.xml.sax.DTDHandler getDTDHandler()

GPathResult getDocument()

@return 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

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(org.xml.sax.InputSource input)

Parse the content of the specified input source into a GPathResult object

GPathResult parse(java.io.File file)

Parses the content of the given file as XML turning it 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)

Parameters:
keepWhitespace - If true then whitespace before elements is kept.

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, Attributes atts)

void startPrefixMapping(java.lang.String tag, java.lang.String uri)

 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
org.xml.sax.helpers.DefaultHandler#error(org.xml.sax.SAXParseException), org.xml.sax.helpers.DefaultHandler#warning(org.xml.sax.SAXParseException), org.xml.sax.helpers.DefaultHandler#setDocumentLocator(org.xml.sax.Locator), org.xml.sax.helpers.DefaultHandler#resolveEntity(java.lang.String, java.lang.String), org.xml.sax.helpers.DefaultHandler#processingInstruction(java.lang.String, java.lang.String), org.xml.sax.helpers.DefaultHandler#startDocument(), org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes), org.xml.sax.helpers.DefaultHandler#characters([C, int, int), org.xml.sax.helpers.DefaultHandler#ignorableWhitespace([C, int, int), org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String), org.xml.sax.helpers.DefaultHandler#endDocument(), org.xml.sax.helpers.DefaultHandler#unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String), org.xml.sax.helpers.DefaultHandler#notationDecl(java.lang.String, java.lang.String, java.lang.String), org.xml.sax.helpers.DefaultHandler#fatalError(org.xml.sax.SAXParseException), org.xml.sax.helpers.DefaultHandler#startPrefixMapping(java.lang.String, java.lang.String), org.xml.sax.helpers.DefaultHandler#endPrefixMapping(java.lang.String), org.xml.sax.helpers.DefaultHandler#skippedEntity(java.lang.String), org.xml.sax.helpers.DefaultHandler#wait(long), org.xml.sax.helpers.DefaultHandler#wait(long, int), org.xml.sax.helpers.DefaultHandler#wait(), org.xml.sax.helpers.DefaultHandler#equals(java.lang.Object), org.xml.sax.helpers.DefaultHandler#toString(), org.xml.sax.helpers.DefaultHandler#hashCode(), org.xml.sax.helpers.DefaultHandler#getClass(), org.xml.sax.helpers.DefaultHandler#notify(), org.xml.sax.helpers.DefaultHandler#notifyAll()
 
Methods inherited from class java.lang.Object
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Constructor Detail

XmlSlurper

public XmlSlurper()
Uses the defaults of not validating and namespace aware.
throws:
ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
throws:
SAXException for SAX errors.


XmlSlurper

public XmlSlurper(boolean validating, boolean namespaceAware)


XmlSlurper

public XmlSlurper(org.xml.sax.XMLReader reader)


XmlSlurper

public XmlSlurper(javax.xml.parsers.SAXParser parser)


 
Method Detail

characters

public void characters(char[] ch, int start, int length)


endDocument

public void endDocument()


endElement

public void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)


getDTDHandler

public org.xml.sax.DTDHandler getDTDHandler()


getDocument

public GPathResult 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


getEntityResolver

public org.xml.sax.EntityResolver getEntityResolver()


getErrorHandler

public org.xml.sax.ErrorHandler getErrorHandler()


getFeature

public boolean getFeature(java.lang.String uri)


getProperty

public java.lang.Object getProperty(java.lang.String uri)


parse

public GPathResult parse(org.xml.sax.InputSource input)
Parse the content of the specified input source into a GPathResult object
throws:
SAXException Any SAX exception, possibly wrapping another exception.
throws:
IOException An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
Parameters:
input - the InputSource to parse
Returns:
An object which supports GPath expressions


parse

public GPathResult parse(java.io.File file)
Parses the content of the given file as XML turning it into a GPathResult object
throws:
SAXException Any SAX exception, possibly wrapping another exception.
throws:
IOException An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
Parameters:
file - the File to parse
Returns:
An object which supports GPath expressions


parse

public GPathResult parse(java.io.InputStream input)
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).
throws:
SAXException Any SAX exception, possibly wrapping another exception.
throws:
IOException An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
Parameters:
input - the InputStream to parse
Returns:
An object which supports GPath expressions


parse

public GPathResult parse(java.io.Reader in)
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).
throws:
SAXException Any SAX exception, possibly wrapping another exception.
throws:
IOException An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
Parameters:
in - the Reader to parse
Returns:
An object which supports GPath expressions


parse

public GPathResult parse(java.lang.String uri)
Parse the content of the specified URI into a GPathResult Object
throws:
SAXException Any SAX exception, possibly wrapping another exception.
throws:
IOException An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
Parameters:
uri - a String containing the URI to parse
Returns:
An object which supports GPath expressions


parseText

public GPathResult parseText(java.lang.String text)
A helper method to parse the given text as XML
throws:
SAXException Any SAX exception, possibly wrapping another exception.
throws:
IOException An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
Parameters:
text - a String containing XML to parse
Returns:
An object which supports GPath expressions


setDTDHandler

public void setDTDHandler(org.xml.sax.DTDHandler dtdHandler)


setEntityBaseUrl

public void setEntityBaseUrl(java.net.URL base)
Resolves entities against using the supplied URL as the base for relative URLs
Parameters:
base - The URL used to resolve relative URLs


setEntityResolver

public void setEntityResolver(org.xml.sax.EntityResolver entityResolver)


setErrorHandler

public void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)


setFeature

public void setFeature(java.lang.String uri, boolean value)


setKeepWhitespace

public void setKeepWhitespace(boolean keepWhitespace)
Parameters:
keepWhitespace - If true then whitespace before elements is kept. The default is to discard the whitespace.


setProperty

public void setProperty(java.lang.String uri, java.lang.Object value)


startDocument

public void startDocument()


startElement

public void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, Attributes atts)


startPrefixMapping

public void startPrefixMapping(java.lang.String tag, java.lang.String uri)


 

Groovy Documentation