public class XmlParser extends Object implements ContentHandler
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 and Description |
---|
XmlParser()
Creates a non-validating and non-namespace-aware
XmlParser which does not allow DOCTYPE declarations in documents. |
XmlParser(boolean validating,
boolean namespaceAware)
Creates a
XmlParser which does not allow DOCTYPE declarations in documents. |
XmlParser(boolean validating,
boolean namespaceAware,
boolean allowDocTypeDeclaration)
Creates a
XmlParser . |
XmlParser(SAXParser parser) |
XmlParser(XMLReader reader) |
Modifier and Type | Method and Description |
---|---|
protected void |
addTextToNode() |
void |
characters(char[] buffer,
int start,
int length) |
protected Node |
createNode(Node parent,
Object name,
Map attributes)
Creates a new node with the given parent, name, and attributes.
|
void |
endDocument() |
void |
endElement(String namespaceURI,
String localName,
String qName) |
void |
endPrefixMapping(String prefix) |
Locator |
getDocumentLocator() |
DTDHandler |
getDTDHandler() |
protected Object |
getElementName(String namespaceURI,
String localName,
String qName)
Return a name given the namespaceURI, localName and qName.
|
EntityResolver |
getEntityResolver() |
ErrorHandler |
getErrorHandler() |
boolean |
getFeature(String uri) |
Object |
getProperty(String uri) |
protected XMLReader |
getXMLReader() |
void |
ignorableWhitespace(char[] buffer,
int start,
int len) |
boolean |
isKeepIgnorableWhitespace()
Returns the current keep ignorable whitespace setting.
|
boolean |
isNamespaceAware()
Determine if namespace handling is enabled.
|
boolean |
isTrimWhitespace()
Returns the current trim whitespace setting.
|
Node |
parse(File file)
Parses the content of the given file as XML turning it into a tree
of Nodes.
|
Node |
parse(InputSource input)
Parse the content of the specified input source into a tree of Nodes.
|
Node |
parse(InputStream input)
Parse the content of the specified input stream into a tree of Nodes.
|
Node |
parse(Reader in)
Parse the content of the specified reader into a tree of Nodes.
|
Node |
parse(String uri)
Parse the content of the specified URI into a tree of Nodes.
|
Node |
parseText(String text)
A helper method to parse the given text as XML.
|
void |
processingInstruction(String target,
String data) |
void |
setDocumentLocator(Locator locator) |
void |
setDTDHandler(DTDHandler dtdHandler) |
void |
setEntityResolver(EntityResolver entityResolver) |
void |
setErrorHandler(ErrorHandler errorHandler) |
void |
setFeature(String uri,
boolean value) |
void |
setKeepIgnorableWhitespace(boolean keepIgnorableWhitespace)
Sets the keep ignorable whitespace setting value.
|
void |
setNamespaceAware(boolean namespaceAware)
Enable and/or disable namespace handling.
|
void |
setProperty(String uri,
Object value) |
void |
setTrimWhitespace(boolean trimWhitespace)
Sets the trim whitespace setting value.
|
void |
skippedEntity(String name) |
void |
startDocument() |
void |
startElement(String namespaceURI,
String localName,
String qName,
Attributes list) |
void |
startPrefixMapping(String prefix,
String namespaceURI) |
public XmlParser() throws ParserConfigurationException, SAXException
XmlParser
which does not allow DOCTYPE declarations in documents.ParserConfigurationException
- if no parser which satisfies the requested configuration can be created.SAXException
- for SAX errors.public XmlParser(boolean validating, boolean namespaceAware) throws ParserConfigurationException, SAXException
XmlParser
which does not allow DOCTYPE declarations in documents.validating
- true
if the parser should validate documents as they are parsed; false otherwise.namespaceAware
- true
if the parser should provide support for XML namespaces; false
otherwise.ParserConfigurationException
- if no parser which satisfies the requested configuration can be created.SAXException
- for SAX errors.public XmlParser(boolean validating, boolean namespaceAware, boolean allowDocTypeDeclaration) throws ParserConfigurationException, SAXException
XmlParser
.validating
- true
if the parser should validate documents as they are parsed; false otherwise.namespaceAware
- true
if the parser should provide support for XML namespaces; false
otherwise.allowDocTypeDeclaration
- true
if the parser should provide support for DOCTYPE declarations; false
otherwise.ParserConfigurationException
- if no parser which satisfies the requested configuration can be created.SAXException
- for SAX errors.public XmlParser(XMLReader reader)
public XmlParser(SAXParser parser) throws SAXException
SAXException
public boolean isTrimWhitespace()
public void setTrimWhitespace(boolean trimWhitespace)
trimWhitespace
- the desired setting valuepublic boolean isKeepIgnorableWhitespace()
public void setKeepIgnorableWhitespace(boolean keepIgnorableWhitespace)
keepIgnorableWhitespace
- the desired new valuepublic Node parse(File file) throws IOException, SAXException
file
- the File containing the XML to be parsedSAXException
- 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.public Node parse(InputSource input) throws IOException, SAXException
input
- the InputSource for the XML to parseSAXException
- 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.public Node parse(InputStream input) throws IOException, SAXException
Note that using this method will not provide the parser with any URI for which to find DTDs etc
input
- an InputStream containing the XML to be parsedSAXException
- 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.public Node parse(Reader in) throws IOException, SAXException
Note that using this method will not provide the parser with any URI for which to find DTDs etc
in
- a Reader to read the XML to be parsedSAXException
- 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.public Node parse(String uri) throws IOException, SAXException
uri
- a String containing a uri pointing to the XML to be parsedSAXException
- 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.public Node parseText(String text) throws IOException, SAXException
text
- the XML text to parseSAXException
- 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.public boolean isNamespaceAware()
public void setNamespaceAware(boolean namespaceAware)
namespaceAware
- the new desired valuepublic DTDHandler getDTDHandler()
public EntityResolver getEntityResolver()
public ErrorHandler getErrorHandler()
public boolean getFeature(String uri) throws SAXNotRecognizedException, SAXNotSupportedException
public Object getProperty(String uri) throws SAXNotRecognizedException, SAXNotSupportedException
public void setDTDHandler(DTDHandler dtdHandler)
public void setEntityResolver(EntityResolver entityResolver)
public void setErrorHandler(ErrorHandler errorHandler)
public void setFeature(String uri, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException
public void setProperty(String uri, Object value) throws SAXNotRecognizedException, SAXNotSupportedException
public void startDocument() throws SAXException
startDocument
in interface ContentHandler
SAXException
public void endDocument() throws SAXException
endDocument
in interface ContentHandler
SAXException
public void startElement(String namespaceURI, String localName, String qName, Attributes list) throws SAXException
startElement
in interface ContentHandler
SAXException
public void endElement(String namespaceURI, String localName, String qName) throws SAXException
endElement
in interface ContentHandler
SAXException
public void characters(char[] buffer, int start, int length) throws SAXException
characters
in interface ContentHandler
SAXException
public void startPrefixMapping(String prefix, String namespaceURI) throws SAXException
startPrefixMapping
in interface ContentHandler
SAXException
public void endPrefixMapping(String prefix) throws SAXException
endPrefixMapping
in interface ContentHandler
SAXException
public void ignorableWhitespace(char[] buffer, int start, int len) throws SAXException
ignorableWhitespace
in interface ContentHandler
SAXException
public void processingInstruction(String target, String data) throws SAXException
processingInstruction
in interface ContentHandler
SAXException
public Locator getDocumentLocator()
public void setDocumentLocator(Locator locator)
setDocumentLocator
in interface ContentHandler
public void skippedEntity(String name) throws SAXException
skippedEntity
in interface ContentHandler
SAXException
protected XMLReader getXMLReader()
protected void addTextToNode()
protected Node createNode(Node parent, Object name, 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 Object getElementName(String namespaceURI, String localName, String qName)
namespaceURI
- the namespace URIlocalName
- the local nameqName
- the qualified name