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'] }
| Modifier and Type | Field and Description |
|---|---|
| private StringBuilder | |
| private boolean | |
| private Locator | |
| private boolean | |
| private Node | |
| private final XMLReader | |
| private final List | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public | XmlParser()
Creates a non-validating and namespace-aware |
| public | XmlParser(boolean
true if the parser should validate documents as they are parsed; false otherwise.true if the parser should provide support for XML namespaces; false otherwise.Creates a |
| public | XmlParser(boolean
true if the parser should validate documents as they are parsed; false otherwise.true if the parser should provide support for XML namespaces; false otherwise.true if the parser should provide support for DOCTYPE declarations; false otherwise.Creates a |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected void | |
| public void | characters(char[]
the characters from the XML document buffer, int the start position in the array start, int the number of characters to read from the array length)Implements org. |
| protected Node | Returns: a new Node instance representing the current nodethe parent node, or null if the node being created is the
root node parent, Object an Object representing the name of the node (typically
an instance of name, Map<K, V> QName)a Map of attribute names to attribute values attributes)Creates a new node with the given parent, name, and attributes. |
| public void | endDocument()
Implements org. |
| public void | endElement(String
the Namespace URI, or the empty string if the
element has no Namespace URI or if Namespace
processing is not being performed namespaceURI, String the local name (without prefix), or the
empty string if Namespace processing is not being
performed localName, String the qualified XML name (with prefix), or the
empty string if qualified names are not available qName)Implements org. |
| public void | endPrefixMapping(String
the prefix that was being mapped.
This is the empty string when a default mapping scope ends. prefix)Implements org. |
| public Locator | |
| public DTDHandler | |
| protected Object | Returns: the newly created representation of the namethe namespace URI namespaceURI, String the local name localName, String the qualified name qName)Return a name given the namespaceURI, localName and qName. |
| public EntityResolver | |
| public ErrorHandler | |
| public boolean | |
| public Object | |
| protected XMLReader | |
| public void | ignorableWhitespace(char[]
the characters from the XML document buffer, int the start position in the array start, int the number of characters to read from the array len)Implements org. |
| public boolean | Returns: true if ignorable whitespace will be kept (default false)Returns the current keep ignorable whitespace setting. |
| public boolean | Returns: true if namespace handling is enabledDetermine if namespace handling is enabled. |
| public boolean | Returns: true if whitespace will be trimmedReturns the current trim whitespace setting. |
| public Node | |
| public Node | Returns: the root node of the parsed tree of Nodesthe InputSource for the XML to parse input)Parse the content of the specified input source into a tree of Nodes. |
| public Node | Returns: the root node of the parsed tree of Nodesan InputStream containing the XML to be parsed input)Parse the content of the specified input stream into a tree of Nodes. |
| public Node | |
| public Node | |
| public Node | |
| public void | processingInstruction(String
the processing instruction target target, String the processing instruction data, or null if
none was supplied. The data does not include any
whitespace separating it from the target data)Implements org. |
| public void | setDocumentLocator(Locator
an object that can return the location of
any SAX document event locator)Implements org. |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | setKeepIgnorableWhitespace(boolean
the desired new value keepIgnorableWhitespace)Sets the keep ignorable whitespace setting value. |
| public void | setNamespaceAware(boolean
the new desired value namespaceAware)Enable and/or disable namespace handling. |
| public void | |
| private static void | |
| public void | setTrimWhitespace(boolean
the desired setting value trimWhitespace)Sets the trim whitespace setting value. |
| public void | skippedEntity(String
the name of the skipped entity. If it is a
parameter entity, the name will begin with '%', and if
it is the external DTD subset, it will be the string
"[dtd]" name)Implements org. |
| public void | startDocument()
Implements org. |
| public void | startElement(String
the Namespace URI, or the empty string if the
element has no Namespace URI or if Namespace
processing is not being performed namespaceURI, String the local name (without prefix), or the
empty string if Namespace processing is not being
performed localName, String the qualified name (with prefix), or the
empty string if qualified names are not available qName, Attributes the attributes attached to the element. If
there are no attributes, it shall be an empty
Attributes object. The value of this object after
startElement returns is undefined list)Implements org. |
| public void | startPrefixMapping(String
the Namespace prefix being declared.
An empty string is used for the default element namespace,
which has no prefix. prefix, String the Namespace URI the prefix is mapped to namespaceURI)Implements org. |
| bodyText | back to summary |
|---|---|
| private StringBuilder bodyText | |
| keepIgnorableWhitespace | back to summary |
|---|---|
| private boolean keepIgnorableWhitespace | |
| locator | back to summary |
|---|---|
| private Locator locator | |
| namespaceAware | back to summary |
|---|---|
| private boolean namespaceAware | |
| parent | back to summary |
|---|---|
| private Node parent | |
| reader | back to summary |
|---|---|
| private final XMLReader reader | |
| stack | back to summary |
|---|---|
| private final List<Node> stack | |
| trimWhitespace | back to summary |
|---|---|
| private boolean trimWhitespace | |
| XmlParser | back to summary |
|---|---|
| public XmlParser() throws ParserConfigurationException, SAXException Creates a non-validating and namespace-aware
| |
| XmlParser | back to summary |
|---|---|
| public XmlParser(boolean validating, boolean namespaceAware) throws ParserConfigurationException, SAXException Creates a
| |
| XmlParser | back to summary |
|---|---|
| public XmlParser(boolean validating, boolean namespaceAware, boolean allowDocTypeDeclaration) throws ParserConfigurationException, SAXException Creates a
| |
| XmlParser | back to summary |
|---|---|
| public XmlParser(XMLReader reader) | |
| XmlParser | back to summary |
|---|---|
| public XmlParser(SAXParser parser) throws SAXException | |
| addTextToNode | back to summary |
|---|---|
| protected void addTextToNode() | |
| characters | back to summary |
|---|---|
| public void characters(char[] buffer, int start, int length) throws SAXException Implements org. Doc from org. Receive notification of character data. The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information. The application must not attempt to read from the array outside of the specified range. Individual characters may consist of more than one Java
Your code should not assume that algorithms using
Note that some parsers will report whitespace in element
content using the
| |
| createNode | back to summary |
|---|---|
| protected Node createNode(Node parent, Object name, Map<K, V> attributes) Creates a new node with the given parent, name, and attributes. The
default implementation returns an instance of
| |
| endDocument | back to summary |
|---|---|
| public void endDocument() throws SAXException Implements org. Doc from org. Receive notification of the end of a document. This method is invoked by the parser to signal it has reached the end of the document after successfully completing the parsing process. After the event, the parser will return the control to the application.
| |
| endElement | back to summary |
|---|---|
| public void endElement(String namespaceURI, String localName, String qName) throws SAXException Implements org. Doc from org. Receive notification of the end of an element. The SAX parser will invoke this method at the end of every
element in the XML document; there will be a corresponding
For information on the names, see startElement.
| |
| endPrefixMapping | back to summary |
|---|---|
| public void endPrefixMapping(String prefix) throws SAXException Implements org. Doc from org. End the scope of a prefix-URI mapping. See
| |
| getDocumentLocator | back to summary |
|---|---|
| public Locator getDocumentLocator() | |
| getDTDHandler | back to summary |
|---|---|
| public DTDHandler getDTDHandler() | |
| getElementName | back to summary |
|---|---|
| protected Object getElementName(String namespaceURI, String localName, String qName) Return a name given the namespaceURI, localName and qName. | |
| getEntityResolver | back to summary |
|---|---|
| public EntityResolver getEntityResolver() | |
| getErrorHandler | back to summary |
|---|---|
| public ErrorHandler getErrorHandler() | |
| getFeature | back to summary |
|---|---|
| public boolean getFeature(final String uri) throws SAXNotRecognizedException, SAXNotSupportedException | |
| getProperty | back to summary |
|---|---|
| public Object getProperty(final String uri) throws SAXNotRecognizedException, SAXNotSupportedException | |
| getXMLReader | back to summary |
|---|---|
| protected XMLReader getXMLReader() | |
| ignorableWhitespace | back to summary |
|---|---|
| public void ignorableWhitespace(char[] buffer, int start, int len) throws SAXException Implements org. Doc from org. Receive notification of ignorable whitespace in element content. Validating Parsers must use this method to report each chunk of whitespace in element content (see the W3C XML 1.0 recommendation, section 2.10): non-validating parsers may also use this method if they are capable of parsing and using content models. SAX parsers may return all contiguous whitespace in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity, so that the Locator provides useful information. The application must not attempt to read from the array outside of the specified range.
| |
| isKeepIgnorableWhitespace | back to summary |
|---|---|
| public boolean isKeepIgnorableWhitespace() Returns the current keep ignorable whitespace setting.
| |
| isNamespaceAware | back to summary |
|---|---|
| public boolean isNamespaceAware() Determine if namespace handling is enabled.
| |
| isTrimWhitespace | back to summary |
|---|---|
| public boolean isTrimWhitespace() Returns the current trim whitespace setting.
| |
| parse | back to summary |
|---|---|
| public Node parse(File file) throws IOException, SAXException Parses the content of the given file as XML turning it into a tree of Nodes.
| |
| parse | back to summary |
|---|---|
| public Node parse(InputSource input) throws IOException, SAXException Parse the content of the specified input source into a tree of Nodes.
| |
| parse | back to summary |
|---|---|
| public Node parse(InputStream input) throws IOException, SAXException Parse 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
| |
| parse | back to summary |
|---|---|
| public Node parse(Reader in) throws IOException, SAXException Parse 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
| |
| parse | back to summary |
|---|---|
| public Node parse(String uri) throws IOException, SAXException Parse the content of the specified URI into a tree of Nodes.
| |
| parseText | back to summary |
|---|---|
| public Node parseText(String text) throws IOException, SAXException A helper method to parse the given text as XML.
| |
| processingInstruction | back to summary |
|---|---|
| public void processingInstruction(String target, String data) throws SAXException Implements org. Doc from org. Receive notification of a processing instruction. The Parser will invoke this method once for each processing instruction found: note that processing instructions may occur before or after the main document element. A SAX parser must never report an XML declaration (XML 1.0, section 2.8) or a text declaration (XML 1.0, section 4.3.1) using this method. Like
| |
| setDocumentLocator | back to summary |
|---|---|
| public void setDocumentLocator(Locator locator) Implements org. Doc from org. Receive an object for locating the origin of SAX document events. SAX parsers are strongly encouraged (though not absolutely required) to supply a locator: if it does so, it must supply the locator to the application by invoking this method before invoking any of the other methods in the ContentHandler interface. The locator allows the application to determine the end position of any document-related event, even if the parser is not reporting an error. Typically, the application will use this information for reporting its own errors (such as character content that does not match an application's business rules). The information returned by the locator is probably not sufficient for use with a search engine. Note that the locator will return correct information only
during the invocation SAX event callbacks after
| |
| setDTDHandler | back to summary |
|---|---|
| public void setDTDHandler(final DTDHandler dtdHandler) | |
| setEntityResolver | back to summary |
|---|---|
| public void setEntityResolver(final EntityResolver entityResolver) | |
| setErrorHandler | back to summary |
|---|---|
| public void setErrorHandler(final ErrorHandler errorHandler) | |
| setFeature | back to summary |
|---|---|
| public void setFeature(final String uri, final boolean value) throws SAXNotRecognizedException, SAXNotSupportedException | |
| setKeepIgnorableWhitespace | back to summary |
|---|---|
| public void setKeepIgnorableWhitespace(boolean keepIgnorableWhitespace) Sets the keep ignorable whitespace setting value.
| |
| setNamespaceAware | back to summary |
|---|---|
| public void setNamespaceAware(boolean namespaceAware) Enable and/or disable namespace handling.
| |
| setProperty | back to summary |
|---|---|
| public void setProperty(final String uri, final Object value) throws SAXNotRecognizedException, SAXNotSupportedException | |
| setQuietly | back to summary |
|---|---|
| private static void setQuietly(SAXParserFactory factory, String feature, boolean value) | |
| setTrimWhitespace | back to summary |
|---|---|
| public void setTrimWhitespace(boolean trimWhitespace) Sets the trim whitespace setting value.
| |
| skippedEntity | back to summary |
|---|---|
| public void skippedEntity(String name) throws SAXException Implements org. Doc from org. Receive notification of a skipped entity. This is not called for entity references within markup constructs such as element start tags or markup declarations. (The XML recommendation requires reporting skipped external entities. SAX also reports internal entity expansion/non-expansion, except within markup constructs.) The Parser will invoke this method each time the entity is
skipped. Non-validating processors may skip entities if they
have not seen the declarations (because, for example, the
entity was declared in an external DTD subset). All processors
may skip external entities, depending on the values of the
| |
| startDocument | back to summary |
|---|---|
| public void startDocument() throws SAXException Implements org. Doc from org. Receive notification of the beginning of a document. The SAX parser will invoke this method only once, before any
other event callbacks (except for
| |
| startElement | back to summary |
|---|---|
| public void startElement(String namespaceURI, String localName, String qName, Attributes list) throws SAXException Implements org. Doc from org. Receive notification of the beginning of an element. The Parser will invoke this method at the beginning of every
element in the XML document; there will be a corresponding
This event allows up to three name components for each element:
Any or all of these may be provided, depending on the values of the http://xml.org/sax/features/namespaces and the http://xml.org/sax/features/namespace-prefixes properties:
Note that the attribute list provided will contain only
attributes with explicit values (specified or defaulted):
#IMPLIED attributes will be omitted. The attribute list
will contain attributes used for Namespace declarations
(xmlns* attributes) only if the
Like
| |
| startPrefixMapping | back to summary |
|---|---|
| public void startPrefixMapping(String prefix, String namespaceURI) throws SAXException Implements org. Doc from org. Begin the scope of a prefix-URI Namespace mapping. The information from this event is not necessary for
normal Namespace processing: the SAX XML reader will
automatically replace prefixes for element and attribute
names when the There are cases, however, when applications need to use prefixes in character data or in attribute values, where they cannot safely be expanded automatically; the start/endPrefixMapping event supplies the information to the application to expand prefixes in those contexts itself, if necessary. Note that start/endPrefixMapping events are not
guaranteed to be properly nested relative to each other:
all startPrefixMapping events will occur immediately before the
corresponding There should never be start/endPrefixMapping events for the "xml" prefix, since it is predeclared and immutable.
| |