groovy.util
Class XmlParser

java.lang.Object
  extended bygroovy.util.XmlParser
All Implemented Interfaces:
ContentHandler

public class XmlParser
extends Object
implements 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 thats 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.

Version:
$Revision: 1.4 $
Author:
James Strachan

Constructor Summary
XmlParser()
           
XmlParser(boolean validating, boolean namespaceAware)
           
XmlParser(SAXParser parser)
           
XmlParser(XMLReader reader)
           
 
Method Summary
protected  void addTextToNode()
           
 void characters(char[] buffer, int start, int length)
           
 void endDocument()
           
 void endElement(String namespaceURI, String localName, String qName)
           
 void endPrefixMapping(String prefix)
           
 Locator getDocumentLocator()
           
protected  Object getElementName(String namespaceURI, String localName, String qName)
           
protected  XMLReader getXMLReader()
           
 void ignorableWhitespace(char[] buffer, int start, int len)
           
 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 skippedEntity(String name)
           
 void startDocument()
           
 void startElement(String namespaceURI, String localName, String qName, Attributes list)
           
 void startPrefixMapping(String prefix, String namespaceURI)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlParser

public XmlParser()
          throws ParserConfigurationException,
                 SAXException

XmlParser

public XmlParser(boolean validating,
                 boolean namespaceAware)
          throws ParserConfigurationException,
                 SAXException

XmlParser

public XmlParser(XMLReader reader)

XmlParser

public XmlParser(SAXParser parser)
          throws SAXException
Method Detail

parse

public Node parse(File file)
           throws IOException,
                  SAXException
Parses the content of the given file as XML turning it into a tree of Nodes

Throws:
IOException
SAXException

parse

public Node parse(InputSource input)
           throws IOException,
                  SAXException
Parse the content of the specified input source into a tree of Nodes.

Throws:
IOException
SAXException

parse

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

Throws:
IOException
SAXException

parse

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

Throws:
IOException
SAXException

parse

public Node parse(String uri)
           throws IOException,
                  SAXException
Parse the content of the specified URI into a tree of Nodes

Throws:
IOException
SAXException

parseText

public Node parseText(String text)
               throws IOException,
                      SAXException
A helper method to parse the given text as XML

Parameters:
text -
Returns:
Throws:
IOException
SAXException

startDocument

public void startDocument()
                   throws SAXException
Specified by:
startDocument in interface ContentHandler
Throws:
SAXException

endDocument

public void endDocument()
                 throws SAXException
Specified by:
endDocument in interface ContentHandler
Throws:
SAXException

startElement

public void startElement(String namespaceURI,
                         String localName,
                         String qName,
                         Attributes list)
                  throws SAXException
Specified by:
startElement in interface ContentHandler
Throws:
SAXException

endElement

public void endElement(String namespaceURI,
                       String localName,
                       String qName)
                throws SAXException
Specified by:
endElement in interface ContentHandler
Throws:
SAXException

characters

public void characters(char[] buffer,
                       int start,
                       int length)
                throws SAXException
Specified by:
characters in interface ContentHandler
Throws:
SAXException

startPrefixMapping

public void startPrefixMapping(String prefix,
                               String namespaceURI)
                        throws SAXException
Specified by:
startPrefixMapping in interface ContentHandler
Throws:
SAXException

endPrefixMapping

public void endPrefixMapping(String prefix)
                      throws SAXException
Specified by:
endPrefixMapping in interface ContentHandler
Throws:
SAXException

ignorableWhitespace

public void ignorableWhitespace(char[] buffer,
                                int start,
                                int len)
                         throws SAXException
Specified by:
ignorableWhitespace in interface ContentHandler
Throws:
SAXException

processingInstruction

public void processingInstruction(String target,
                                  String data)
                           throws SAXException
Specified by:
processingInstruction in interface ContentHandler
Throws:
SAXException

getDocumentLocator

public Locator getDocumentLocator()

setDocumentLocator

public void setDocumentLocator(Locator locator)
Specified by:
setDocumentLocator in interface ContentHandler

skippedEntity

public void skippedEntity(String name)
                   throws SAXException
Specified by:
skippedEntity in interface ContentHandler
Throws:
SAXException

getXMLReader

protected XMLReader getXMLReader()

addTextToNode

protected void addTextToNode()

getElementName

protected Object getElementName(String namespaceURI,
                                String localName,
                                String qName)
                         throws SAXException
Throws:
SAXException


Copyright © 2003-2007 The Codehaus. All Rights Reserved.