public class XmlParser
extends java.lang.Object
implements org.xml.sax.ContentHandler
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 and Description |
|---|
XmlParser() |
XmlParser(boolean validating,
boolean namespaceAware) |
XmlParser(javax.xml.parsers.SAXParser parser) |
XmlParser(org.xml.sax.XMLReader reader) |
| Modifier and Type | Method and Description |
|---|---|
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.Locator |
getDocumentLocator() |
org.xml.sax.DTDHandler |
getDTDHandler() |
protected java.lang.Object |
getElementName(java.lang.String namespaceURI,
java.lang.String localName,
java.lang.String qName)
Return a name given the namespaceURI, localName and 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 |
setDocumentLocator(org.xml.sax.Locator locator) |
void |
setDTDHandler(org.xml.sax.DTDHandler dtdHandler) |
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,
org.xml.sax.Attributes list) |
void |
startPrefixMapping(java.lang.String prefix,
java.lang.String namespaceURI) |
public XmlParser()
throws javax.xml.parsers.ParserConfigurationException,
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXExceptionpublic XmlParser(boolean validating,
boolean namespaceAware)
throws javax.xml.parsers.ParserConfigurationException,
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXExceptionpublic XmlParser(org.xml.sax.XMLReader reader)
public XmlParser(javax.xml.parsers.SAXParser parser)
throws org.xml.sax.SAXException
org.xml.sax.SAXExceptionpublic boolean isTrimWhitespace()
public void setTrimWhitespace(boolean trimWhitespace)
trimWhitespace - the desired setting valuepublic Node parse(java.io.File file) throws java.io.IOException, org.xml.sax.SAXException
file - the File containing the XML to be parsedorg.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 Node parse(org.xml.sax.InputSource input) throws java.io.IOException, org.xml.sax.SAXException
input - the InputSource for the 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 Node parse(java.io.InputStream input) throws java.io.IOException, org.xml.sax.SAXException
input - an InputStream containing the XML to be parsedorg.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 Node parse(java.io.Reader in) throws java.io.IOException, org.xml.sax.SAXException
in - a Reader to read the XML to be parsedorg.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 Node parse(java.lang.String uri) throws java.io.IOException, org.xml.sax.SAXException
uri - a String containing a uri pointing to the XML to be parsedorg.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 Node parseText(java.lang.String text) throws java.io.IOException, org.xml.sax.SAXException
text - the XML text 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 boolean isNamespaceAware()
public void setNamespaceAware(boolean namespaceAware)
namespaceAware - the new desired valuepublic 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.SAXNotRecognizedExceptionorg.xml.sax.SAXNotSupportedExceptionpublic java.lang.Object getProperty(java.lang.String uri)
throws org.xml.sax.SAXNotRecognizedException,
org.xml.sax.SAXNotSupportedException
org.xml.sax.SAXNotRecognizedExceptionorg.xml.sax.SAXNotSupportedExceptionpublic void setDTDHandler(org.xml.sax.DTDHandler dtdHandler)
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)
throws org.xml.sax.SAXNotRecognizedException,
org.xml.sax.SAXNotSupportedException
org.xml.sax.SAXNotRecognizedExceptionorg.xml.sax.SAXNotSupportedExceptionpublic void setProperty(java.lang.String uri,
java.lang.Object value)
throws org.xml.sax.SAXNotRecognizedException,
org.xml.sax.SAXNotSupportedException
org.xml.sax.SAXNotRecognizedExceptionorg.xml.sax.SAXNotSupportedExceptionpublic void startDocument()
throws org.xml.sax.SAXException
startDocument in interface org.xml.sax.ContentHandlerorg.xml.sax.SAXExceptionpublic void endDocument()
throws org.xml.sax.SAXException
endDocument in interface org.xml.sax.ContentHandlerorg.xml.sax.SAXExceptionpublic void startElement(java.lang.String namespaceURI,
java.lang.String localName,
java.lang.String qName,
org.xml.sax.Attributes list)
throws org.xml.sax.SAXException
startElement in interface org.xml.sax.ContentHandlerorg.xml.sax.SAXExceptionpublic 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.ContentHandlerorg.xml.sax.SAXExceptionpublic void characters(char[] buffer,
int start,
int length)
throws org.xml.sax.SAXException
characters in interface org.xml.sax.ContentHandlerorg.xml.sax.SAXExceptionpublic void startPrefixMapping(java.lang.String prefix,
java.lang.String namespaceURI)
throws org.xml.sax.SAXException
startPrefixMapping in interface org.xml.sax.ContentHandlerorg.xml.sax.SAXExceptionpublic void endPrefixMapping(java.lang.String prefix)
throws org.xml.sax.SAXException
endPrefixMapping in interface org.xml.sax.ContentHandlerorg.xml.sax.SAXExceptionpublic void ignorableWhitespace(char[] buffer,
int start,
int len)
throws org.xml.sax.SAXException
ignorableWhitespace in interface org.xml.sax.ContentHandlerorg.xml.sax.SAXExceptionpublic void processingInstruction(java.lang.String target,
java.lang.String data)
throws org.xml.sax.SAXException
processingInstruction in interface org.xml.sax.ContentHandlerorg.xml.sax.SAXExceptionpublic org.xml.sax.Locator getDocumentLocator()
public void setDocumentLocator(org.xml.sax.Locator locator)
setDocumentLocator in interface org.xml.sax.ContentHandlerpublic void skippedEntity(java.lang.String name)
throws org.xml.sax.SAXException
skippedEntity in interface org.xml.sax.ContentHandlerorg.xml.sax.SAXExceptionprotected org.xml.sax.XMLReader getXMLReader()
protected void addTextToNode()
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 valuesprotected java.lang.Object getElementName(java.lang.String namespaceURI,
java.lang.String localName,
java.lang.String qName)
namespaceURI - the namespace URIlocalName - the local nameqName - the qualified name