Package groovy.xml
Class XmlParser
java.lang.Object
groovy.xml.XmlParser
- All Implemented Interfaces:
- ContentHandler
A helper class for parsing XML into a tree of Node instances for a
 simple way of processing XML. This parser does not preserve the XML
 InfoSet - if that's what you need try using W3C DOM, dom4j, JDOM, XOM etc.
 This parser ignores comments and processing instructions and converts
 the XML into a Node for each element in the XML with attributes
 and child Nodes and Strings. This simple model is sufficient for
 most simple use cases of processing XML.
 
Example usage:
 import groovy.xml.XmlParser
 def xml = '<root><one a1="uno!"/><two>Some text!</two></root>'
 def rootNode = new XmlParser().parseText(xml)
 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'] }
 - 
Constructor SummaryConstructorsConstructorDescriptionCreates a non-validating and namespace-awareXmlParserwhich does not allow DOCTYPE declarations in documents.XmlParser(boolean validating, boolean namespaceAware) Creates aXmlParserwhich does not allow DOCTYPE declarations in documents.XmlParser(boolean validating, boolean namespaceAware, boolean allowDocTypeDeclaration) Creates aXmlParser.
- 
Method SummaryModifier and TypeMethodDescriptionprotected voidvoidcharacters(char[] buffer, int start, int length) protected NodecreateNode(Node parent, Object name, Map attributes) Creates a new node with the given parent, name, and attributes.voidvoidendElement(String namespaceURI, String localName, String qName) voidendPrefixMapping(String prefix) protected ObjectgetElementName(String namespaceURI, String localName, String qName) Return a name given the namespaceURI, localName and qName.booleangetFeature(String uri) getProperty(String uri) protected XMLReadervoidignorableWhitespace(char[] buffer, int start, int len) booleanReturns the current keep ignorable whitespace setting.booleanDetermine if namespace handling is enabled.booleanReturns the current trim whitespace setting.Parses the content of the given file as XML turning it into a tree of Nodes.parse(InputStream input) Parse the content of the specified input stream into a tree of Nodes.Parse the content of the specified reader into a tree of Nodes.Parse the content of the specified URI into a tree of Nodes.parse(InputSource input) Parse the content of the specified input source into a tree of Nodes.A helper method to parse the given text as XML.voidprocessingInstruction(String target, String data) voidsetDocumentLocator(Locator locator) voidsetDTDHandler(DTDHandler dtdHandler) voidsetEntityResolver(EntityResolver entityResolver) voidsetErrorHandler(ErrorHandler errorHandler) voidsetFeature(String uri, boolean value) voidsetKeepIgnorableWhitespace(boolean keepIgnorableWhitespace) Sets the keep ignorable whitespace setting value.voidsetNamespaceAware(boolean namespaceAware) Enable and/or disable namespace handling.voidsetProperty(String uri, Object value) voidsetTrimWhitespace(boolean trimWhitespace) Sets the trim whitespace setting value.voidskippedEntity(String name) voidvoidstartElement(String namespaceURI, String localName, String qName, Attributes list) voidstartPrefixMapping(String prefix, String namespaceURI) Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.xml.sax.ContentHandlerdeclaration
- 
Constructor Details- 
XmlParserCreates a non-validating and namespace-awareXmlParserwhich does not allow DOCTYPE declarations in documents.- Throws:
- ParserConfigurationException- if no parser which satisfies the requested configuration can be created.
- SAXException- for SAX errors.
 
- 
XmlParserpublic XmlParser(boolean validating, boolean namespaceAware) throws ParserConfigurationException, SAXException Creates aXmlParserwhich 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.
 
- 
XmlParserpublic XmlParser(boolean validating, boolean namespaceAware, boolean allowDocTypeDeclaration) throws ParserConfigurationException, SAXException Creates aXmlParser.- 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.
 
- 
XmlParser
- 
XmlParser- Throws:
- SAXException
 
 
- 
- 
Method Details- 
isTrimWhitespacepublic boolean isTrimWhitespace()Returns the current trim whitespace setting.- Returns:
- true if whitespace will be trimmed
 
- 
setTrimWhitespacepublic void setTrimWhitespace(boolean trimWhitespace) Sets the trim whitespace setting value.- Parameters:
- trimWhitespace- the desired setting value
 
- 
isKeepIgnorableWhitespacepublic boolean isKeepIgnorableWhitespace()Returns the current keep ignorable whitespace setting.- Returns:
- true if ignorable whitespace will be kept (default false)
 
- 
setKeepIgnorableWhitespacepublic void setKeepIgnorableWhitespace(boolean keepIgnorableWhitespace) Sets the keep ignorable whitespace setting value.- Parameters:
- keepIgnorableWhitespace- the desired new value
 
- 
parseParses the content of the given file as XML turning it into a tree of Nodes.- Parameters:
- file- the File containing the XML to be parsed
- Returns:
- the root node of the parsed tree of Nodes
- 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.
 
- 
parseParse the content of the specified input source into a tree of Nodes.- Parameters:
- input- the InputSource for the XML to parse
- Returns:
- the root node of the parsed tree of Nodes
- 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.
 
- 
parseParse the content of the specified input stream into a tree of Nodes.Note that using this method will not provide the parser with any URI for which to find DTDs etc - Parameters:
- input- an InputStream containing the XML to be parsed
- Returns:
- the root node of the parsed tree of Nodes
- 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.
 
- 
parseParse the content of the specified reader into a tree of Nodes.Note that using this method will not provide the parser with any URI for which to find DTDs etc - Parameters:
- in- a Reader to read the XML to be parsed
- Returns:
- the root node of the parsed tree of Nodes
- 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.
 
- 
parseParse the content of the specified URI into a tree of Nodes.- Parameters:
- uri- a String containing a URI pointing to the XML to be parsed
- Returns:
- the root node of the parsed tree of Nodes
- 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.
 
- 
parseTextA helper method to parse the given text as XML.- Parameters:
- text- the XML text to parse
- Returns:
- the root node of the parsed tree of Nodes
- 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.
 
- 
isNamespaceAwarepublic boolean isNamespaceAware()Determine if namespace handling is enabled.- Returns:
- true if namespace handling is enabled
 
- 
setNamespaceAwarepublic void setNamespaceAware(boolean namespaceAware) Enable and/or disable namespace handling.- Parameters:
- namespaceAware- the new desired value
 
- 
getDTDHandler
- 
getEntityResolver
- 
getErrorHandler
- 
getFeature
- 
getProperty
- 
setDTDHandler
- 
setEntityResolver
- 
setErrorHandler
- 
setFeaturepublic void setFeature(String uri, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException 
- 
setPropertypublic void setProperty(String uri, Object value) throws SAXNotRecognizedException, SAXNotSupportedException 
- 
startDocument- Specified by:
- startDocumentin interface- ContentHandler
- Throws:
- SAXException
 
- 
endDocument- Specified by:
- endDocumentin interface- ContentHandler
- Throws:
- SAXException
 
- 
startElementpublic void startElement(String namespaceURI, String localName, String qName, Attributes list) throws SAXException - Specified by:
- startElementin interface- ContentHandler
- Throws:
- SAXException
 
- 
endElement- Specified by:
- endElementin interface- ContentHandler
- Throws:
- SAXException
 
- 
characters- Specified by:
- charactersin interface- ContentHandler
- Throws:
- SAXException
 
- 
startPrefixMapping- Specified by:
- startPrefixMappingin interface- ContentHandler
- Throws:
- SAXException
 
- 
endPrefixMapping- Specified by:
- endPrefixMappingin interface- ContentHandler
- Throws:
- SAXException
 
- 
ignorableWhitespace- Specified by:
- ignorableWhitespacein interface- ContentHandler
- Throws:
- SAXException
 
- 
processingInstruction- Specified by:
- processingInstructionin interface- ContentHandler
- Throws:
- SAXException
 
- 
getDocumentLocator
- 
setDocumentLocator- Specified by:
- setDocumentLocatorin interface- ContentHandler
 
- 
skippedEntity- Specified by:
- skippedEntityin interface- ContentHandler
- Throws:
- SAXException
 
- 
getXMLReader
- 
addTextToNodeprotected void addTextToNode()
- 
createNodeCreates a new node with the given parent, name, and attributes. The default implementation returns an instance ofgroovy.util.Node.- Parameters:
- parent- the parent node, or null if the node being created is the root node
- name- an Object representing the name of the node (typically an instance of- QName)
- attributes- a Map of attribute names to attribute values
- Returns:
- a new Node instance representing the current node
 
- 
getElementNameReturn a name given the namespaceURI, localName and qName.- Parameters:
- namespaceURI- the namespace URI
- localName- the local name
- qName- the qualified name
- Returns:
- the newly created representation of the name
 
 
-