|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object groovy.util.XmlParser
public class XmlParser extends java.lang.Object
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: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 Summary | |
XmlParser()
|
|
XmlParser(boolean validating, boolean namespaceAware)
|
|
XmlParser(org.xml.sax.XMLReader reader)
|
|
XmlParser(javax.xml.parsers.SAXParser parser)
|
Method Summary | |
---|---|
protected void
|
addTextToNode()
|
void
|
characters(char[] buffer, int start, int length)
|
protected Node
|
createNode(Node parent, java.lang.Object name, java.util.Map attributes)
Creates a new node with the given parent, name, and attributes. |
void
|
endDocument()
|
void
|
endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
|
void
|
endPrefixMapping(java.lang.String prefix)
|
org.xml.sax.DTDHandler
|
getDTDHandler()
|
org.xml.sax.Locator
|
getDocumentLocator()
|
protected java.lang.Object
|
getElementName(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
|
org.xml.sax.EntityResolver
|
getEntityResolver()
|
org.xml.sax.ErrorHandler
|
getErrorHandler()
|
boolean
|
getFeature(java.lang.String uri)
|
java.lang.Object
|
getProperty(java.lang.String uri)
|
protected org.xml.sax.XMLReader
|
getXMLReader()
|
void
|
ignorableWhitespace(char[] buffer, int start, int len)
|
boolean
|
isNamespaceAware()
Determine if namespace handling is enabled. |
boolean
|
isTrimWhitespace()
Returns the current trim whitespace setting. |
Node
|
parse(java.io.File file)
Parses the content of the given file as XML turning it into a tree of Nodes. |
Node
|
parse(org.xml.sax.InputSource input)
Parse the content of the specified input source into a tree of Nodes. |
Node
|
parse(java.io.InputStream input)
Parse the content of the specified input stream into a tree of Nodes. |
Node
|
parse(java.io.Reader in)
Parse the content of the specified reader into a tree of Nodes. |
Node
|
parse(java.lang.String uri)
Parse the content of the specified URI into a tree of Nodes. |
Node
|
parseText(java.lang.String text)
A helper method to parse the given text as XML. |
void
|
processingInstruction(java.lang.String target, java.lang.String data)
|
void
|
setDTDHandler(org.xml.sax.DTDHandler dtdHandler)
|
void
|
setDocumentLocator(org.xml.sax.Locator locator)
|
void
|
setEntityResolver(org.xml.sax.EntityResolver entityResolver)
|
void
|
setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
|
void
|
setFeature(java.lang.String uri, boolean value)
|
void
|
setNamespaceAware(boolean namespaceAware)
Enable and/or disable namespace handling. |
void
|
setProperty(java.lang.String uri, java.lang.Object value)
|
void
|
setTrimWhitespace(boolean trimWhitespace)
Sets the trim whitespace setting value. |
void
|
skippedEntity(java.lang.String name)
|
void
|
startDocument()
|
void
|
startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, Attributes list)
|
void
|
startPrefixMapping(java.lang.String prefix, java.lang.String namespaceURI)
|
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 |
---|
public XmlParser()
public XmlParser(boolean validating, boolean namespaceAware)
public XmlParser(org.xml.sax.XMLReader reader)
public XmlParser(javax.xml.parsers.SAXParser parser)
Method Detail |
---|
protected void addTextToNode()
public void characters(char[] buffer, int start, int length)
protected Node createNode(Node parent, java.lang.Object name, java.util.Map attributes)
groovy.util.Node
.
parent
- the parent node, or null if the node being created is the
root nodename
- an Object representing the name of the node (typically
an instance of QName)attributes
- a Map of attribute names to attribute values
public void endDocument()
public void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
public void endPrefixMapping(java.lang.String prefix)
public org.xml.sax.DTDHandler getDTDHandler()
public org.xml.sax.Locator getDocumentLocator()
protected java.lang.Object getElementName(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
public org.xml.sax.EntityResolver getEntityResolver()
public org.xml.sax.ErrorHandler getErrorHandler()
public boolean getFeature(java.lang.String uri)
public java.lang.Object getProperty(java.lang.String uri)
protected org.xml.sax.XMLReader getXMLReader()
public void ignorableWhitespace(char[] buffer, int start, int len)
public boolean isNamespaceAware()
public boolean isTrimWhitespace()
public Node parse(java.io.File file)
file
- the File containing the XML to be parsed
public Node parse(org.xml.sax.InputSource input)
input
- the InputSource for the XML to parse
public Node parse(java.io.InputStream input)
input
- an InputStream containing the XML to be parsed
public Node parse(java.io.Reader in)
in
- a Reader to read the XML to be parsed
public Node parse(java.lang.String uri)
uri
- a String containing a uri pointing to the XML to be parsed
public Node parseText(java.lang.String text)
text
- the XML text to parse
public void processingInstruction(java.lang.String target, java.lang.String data)
public void setDTDHandler(org.xml.sax.DTDHandler dtdHandler)
public void setDocumentLocator(org.xml.sax.Locator locator)
public void setEntityResolver(org.xml.sax.EntityResolver entityResolver)
public void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
public void setFeature(java.lang.String uri, boolean value)
public void setNamespaceAware(boolean namespaceAware)
namespaceAware
- the new desired value
public void setProperty(java.lang.String uri, java.lang.Object value)
public void setTrimWhitespace(boolean trimWhitespace)
trimWhitespace
- the desired setting value
public void skippedEntity(java.lang.String name)
public void startDocument()
public void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, Attributes list)
public void startPrefixMapping(java.lang.String prefix, java.lang.String namespaceURI)
Groovy Documentation