groovy.util
Class XmlParser

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

public class XmlParser
extends java.lang.Object
implements org.xml.sax.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

Field Summary
private  java.lang.StringBuffer bodyText
           
private  org.xml.sax.Locator locator
           
private  Node parent
           
private  org.xml.sax.XMLReader reader
           
private  java.util.List stack
           
private  boolean trimWhitespace
           
 
Constructor Summary
XmlParser()
           
XmlParser(boolean validating, boolean namespaceAware)
           
XmlParser(javax.xml.parsers.SAXParser parser)
           
XmlParser(org.xml.sax.XMLReader reader)
           
 
Method Summary
protected  void addTextToNode()
           
 void characters(char[] buffer, int start, int length)
           
 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()
           
protected  java.lang.Object getElementName(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
           
protected  org.xml.sax.XMLReader getXMLReader()
           
 void ignorableWhitespace(char[] buffer, int start, int len)
           
 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 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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bodyText

private java.lang.StringBuffer bodyText

stack

private java.util.List stack

locator

private org.xml.sax.Locator locator

reader

private org.xml.sax.XMLReader reader

parent

private Node parent

trimWhitespace

private boolean trimWhitespace
Constructor Detail

XmlParser

public XmlParser()
          throws javax.xml.parsers.ParserConfigurationException,
                 org.xml.sax.SAXException

XmlParser

public XmlParser(boolean validating,
                 boolean namespaceAware)
          throws javax.xml.parsers.ParserConfigurationException,
                 org.xml.sax.SAXException

XmlParser

public XmlParser(org.xml.sax.XMLReader reader)

XmlParser

public XmlParser(javax.xml.parsers.SAXParser parser)
          throws org.xml.sax.SAXException
Method Detail

parse

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

Throws:
java.io.IOException
org.xml.sax.SAXException

parse

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

Throws:
java.io.IOException
org.xml.sax.SAXException

parse

public Node parse(java.io.InputStream input)
           throws java.io.IOException,
                  org.xml.sax.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:
java.io.IOException
org.xml.sax.SAXException

parse

public Node parse(java.io.Reader in)
           throws java.io.IOException,
                  org.xml.sax.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:
java.io.IOException
org.xml.sax.SAXException

parse

public Node parse(java.lang.String uri)
           throws java.io.IOException,
                  org.xml.sax.SAXException
Parse the content of the specified URI into a tree of Nodes

Throws:
java.io.IOException
org.xml.sax.SAXException

parseText

public Node parseText(java.lang.String text)
               throws java.io.IOException,
                      org.xml.sax.SAXException
A helper method to parse the given text as XML

Parameters:
text -
Returns:
Throws:
java.io.IOException
org.xml.sax.SAXException

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Specified by:
startDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Specified by:
endDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

startElement

public void startElement(java.lang.String namespaceURI,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes list)
                  throws org.xml.sax.SAXException
Specified by:
startElement in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String namespaceURI,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
Specified by:
endElement in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

characters

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

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String namespaceURI)
                        throws org.xml.sax.SAXException
Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

endPrefixMapping

public void endPrefixMapping(java.lang.String prefix)
                      throws org.xml.sax.SAXException
Specified by:
endPrefixMapping in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

ignorableWhitespace

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

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Specified by:
processingInstruction in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

getDocumentLocator

public org.xml.sax.Locator getDocumentLocator()

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler

skippedEntity

public void skippedEntity(java.lang.String name)
                   throws org.xml.sax.SAXException
Specified by:
skippedEntity in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

getXMLReader

protected org.xml.sax.XMLReader getXMLReader()

addTextToNode

protected void addTextToNode()

getElementName

protected java.lang.Object getElementName(java.lang.String namespaceURI,
                                          java.lang.String localName,
                                          java.lang.String qName)
                                   throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException


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