com.synex.xml.sax
Class XMLParser

java.lang.Object
  |
  +--org.xml.sax.helpers.DefaultHandler
        |
        +--com.synex.xml.sax.XMLParser
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, org.xml.sax.ext.LexicalHandler

public class XMLParser
extends org.xml.sax.helpers.DefaultHandler
implements org.xml.sax.ext.LexicalHandler

The class reads XML documents according to specified DTD and translates all related events into XMLHandler events.

Usage sample:

    XMLParser parser = new XMLParser(...);
    parser.parse(new InputSource("..."));
 


Constructor Summary
XMLParser()
          Creates new XMLParser
XMLParser(XMLHandler handler)
          Creates new XML Parser
 
Method Summary
 void characters(char[] buf, int offset, int len)
          Receive notification of character data.
 void comment(char[] ch, int start, int length)
          Report an XML comment anywhere in the document.
 void endCDATA()
          Report the end of a CDATA section.
 void endDocument()
          Receive notification of the end of the document.
 void endDTD()
          Report the end of DTD declarations.
 void endElement(java.lang.String namespaceURI, java.lang.String sName, java.lang.String qName)
          Receive notification of the end of an element.
 void endEntity(java.lang.String name)
          Report the end of an entity.
 void ignorableWhitespace(char[] buf, int offset, int len)
          Receive notification of ignorable whitespace in element content.
static void main(java.lang.String[] argv)
          The main method
 void parse(org.xml.sax.InputSource input)
          The recognizer entry method taking an InputSource.
 void parse(org.xml.sax.InputSource input, XMLHandler handler)
          The recognizer entry method taking an Inputsource.
 void parse(java.net.URL url)
          The recognizer entry method taking a URL.
 void parse(java.net.URL url, XMLHandler handler)
          The recognizer entry method taking a URL.
 void processingInstruction(java.lang.String target, java.lang.String data)
          Receive notification of a processing instruction.
 void setDocumentLocator(org.xml.sax.Locator locator)
          Receive a Locator object for document events.
 void setEcho(boolean pEcho)
          Specifies that the parser will log the parser events to System.out
 void setNamespaceAware(boolean pNamespace)
          Specifies that the parser will provide support for XML namespaces.
 void setValidating(boolean pValidating)
          Specifies that the parser will validate documents as they are parsed.
 void startCDATA()
          Report the start of a CDATA section.
 void startDocument()
          Receive notification of the beginning of the document.
 void startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
          Report the start of DTD declarations, if any.
 void startElement(java.lang.String namespaceURI, java.lang.String sName, java.lang.String qName, org.xml.sax.Attributes attrs)
          Receive notification of the start of an element.
 void startEntity(java.lang.String name)
          Report the beginning of some internal and external XML entities.
static void usage()
           
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, error, fatalError, notationDecl, resolveEntity, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLParser

public XMLParser()
Creates new XMLParser

XMLParser

public XMLParser(XMLHandler handler)
Creates new XML Parser
Parameters:
handler - The SAX Handler to use
Method Detail

setEcho

public void setEcho(boolean pEcho)
Specifies that the parser will log the parser events to System.out
Parameters:
pEcho - True if the parser will log events; false otherwise.

setValidating

public void setValidating(boolean pValidating)
Specifies that the parser will validate documents as they are parsed. By default the value of this is set to true.
Parameters:
pValidating - True if the parser will validate documents as they are parsed; false otherwise.

setNamespaceAware

public void setNamespaceAware(boolean pNamespace)
Specifies that the parser will provide support for XML namespaces. By default the value of this is set to false.
Parameters:
pNamespace - True if the parser will provide support for XML namespaces; false otherwise.

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Receive a Locator object for document events.
Overrides:
setDocumentLocator in class org.xml.sax.helpers.DefaultHandler
Parameters:
locator - A locator for all SAX document events.

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Receive notification of the beginning of the document.
Overrides:
startDocument in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Receive notification of the end of the document.
Overrides:
endDocument in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

startElement

public void startElement(java.lang.String namespaceURI,
                         java.lang.String sName,
                         java.lang.String qName,
                         org.xml.sax.Attributes attrs)
                  throws org.xml.sax.SAXException
Receive notification of the start of an element. The Parser will invoke this method at the beginning of every element in the XML document; there will be a corresponding endElement event for every startElement event (even when the element is empty). All of the element's content will be reported, in order, before the corresponding endElement event.
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Parameters:
namespaceURI - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
sName - The local name (without prefix), or the empty string if Namespace processing is not being performed.
qName - The qualified name (with prefix), or the empty string if qualified names are not available.
attrs - The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

endElement

public void endElement(java.lang.String namespaceURI,
                       java.lang.String sName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
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 startElement event for every endElement event (even when the element is empty).
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Parameters:
namespaceURI - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
sName - The local name (without prefix), or the empty string if Namespace processing is not being performed.
qName - The qualified XML 1.0 name (with prefix), or the empty string if qualified names are not available.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

characters

public void characters(char[] buf,
                       int offset,
                       int len)
                throws org.xml.sax.SAXException
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.
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler
Parameters:
buf - The characters from the XML document.
offset - The start position in the array.
len - The number of characters to read from the array.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

ignorableWhitespace

public void ignorableWhitespace(char[] buf,
                                int offset,
                                int len)
                         throws org.xml.sax.SAXException
Receive notification of ignorable whitespace in element content.
Overrides:
ignorableWhitespace in class org.xml.sax.helpers.DefaultHandler
Parameters:
buf - The characters from the XML document.
offset - The start position in the array.
len - The number of characters to read from the array.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
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.
Overrides:
processingInstruction in class org.xml.sax.helpers.DefaultHandler
Parameters:
target - The processing instruction target.
data - The processing instruction data, or null if none was supplied. The data does not include
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

comment

public void comment(char[] ch,
                    int start,
                    int length)
             throws org.xml.sax.SAXException
Report an XML comment anywhere in the document. This callback will be used for comments inside or outside the document element, including comments in the external DTD subset (if read). Comments in the DTD must be properly nested inside start/endDTD and start/endEntity events (if used).
Specified by:
comment in interface org.xml.sax.ext.LexicalHandler
Parameters:
ch - An array holding the characters in the comment.
start - The starting position in the array.
length - The number of characters to use from the array.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

endCDATA

public void endCDATA()
              throws org.xml.sax.SAXException
Report the end of a CDATA section.
Specified by:
endCDATA in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

endEntity

public void endEntity(java.lang.String name)
               throws org.xml.sax.SAXException
Report the end of an entity.
Specified by:
endEntity in interface org.xml.sax.ext.LexicalHandler
Parameters:
name - The name of the entity that is ending.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

startEntity

public void startEntity(java.lang.String name)
                 throws org.xml.sax.SAXException
Report the beginning of some internal and external XML entities. The reporting of parameter entities (including the external DTD subset) is optional, and SAX2 drivers that support LexicalHandler may not support it; you can use the http://xml.org/sax/features/lexical-handler/parameter-entities feature to query or control the reporting of parameter entities. General entities are reported with their regular names, parameter entities have '%' prepended to their names, and the external DTD subset has the pseudo-entity name "[dtd]". All start/endEntity events must be properly nested.
Specified by:
startEntity in interface org.xml.sax.ext.LexicalHandler
Parameters:
name - The name of the entity. If it is a parameter entity, the name will begin with '%', and if it is the external DTD subset, it will be "[dtd]".
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

endDTD

public void endDTD()
            throws org.xml.sax.SAXException
Report the end of DTD declarations. This method is intended to report the end of the DOCTYPE declaration; if the document has no DOCTYPE declaration, this method will not be invoked.
Specified by:
endDTD in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

startCDATA

public void startCDATA()
                throws org.xml.sax.SAXException
Report the start of a CDATA section. The contents of the CDATA section will be reported through the regular characters event; this event is intended only to report the boundary.
Specified by:
startCDATA in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

startDTD

public void startDTD(java.lang.String name,
                     java.lang.String publicId,
                     java.lang.String systemId)
              throws org.xml.sax.SAXException
Report the start of DTD declarations, if any. This method is intended to report the beginning of the DOCTYPE declaration; if the document has no DOCTYPE declaration, this method will not be invoked. Note that the start/endDTD events will appear within the start/endDocument events from ContentHandler and before the first startElement event.
Specified by:
startDTD in interface org.xml.sax.ext.LexicalHandler
Parameters:
name - The document type name.
publicId - The declared public identifier for the external DTD subset, or null if none was declared.
systemId - The declared system identifier for the external DTD subset, or null if none was declared.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

parse

public void parse(org.xml.sax.InputSource input)
           throws org.xml.sax.SAXException,
                  javax.xml.parsers.ParserConfigurationException,
                  java.io.IOException
The recognizer entry method taking an InputSource.
Parameters:
input - InputSource to be parsed.
Throws:
java.io.IOException - on I/O error.
org.xml.sax.SAXException - propagated exception thrown by a DocumentHandler.
javax.xml.parsers.ParserConfigurationException - a parser satisfining requested configuration can not be created.

parse

public void parse(java.net.URL url)
           throws org.xml.sax.SAXException,
                  javax.xml.parsers.ParserConfigurationException,
                  java.io.IOException
The recognizer entry method taking a URL.
Parameters:
url - URL source to be parsed.
Throws:
java.io.IOException - on I/O error.
org.xml.sax.SAXException - propagated exception thrown by a DocumentHandler.
javax.xml.parsers.ParserConfigurationException - a parser satisfining requested configuration can not be created.

parse

public void parse(org.xml.sax.InputSource input,
                  XMLHandler handler)
           throws org.xml.sax.SAXException,
                  javax.xml.parsers.ParserConfigurationException,
                  java.io.IOException
The recognizer entry method taking an Inputsource.
Parameters:
input - InputSource to be parsed.
handler - The SAX XMLHandler to use.
Throws:
java.io.IOException - on I/O error.
org.xml.sax.SAXException - propagated exception thrown by a DocumentHandler.
javax.xml.parsers.ParserConfigurationException - a parser satisfining requested configuration can not be created.

parse

public void parse(java.net.URL url,
                  XMLHandler handler)
           throws org.xml.sax.SAXException,
                  javax.xml.parsers.ParserConfigurationException,
                  java.io.IOException
The recognizer entry method taking a URL.
Parameters:
url - URL source to be parsed.
handler - The SAX XMLHandler to use.
Throws:
java.io.IOException - on I/O error.
org.xml.sax.SAXException - propagated exception thrown by a DocumentHandler.
javax.xml.parsers.ParserConfigurationException - a parser satisfining requested configuration can not be created.

usage

public static void usage()

main

public static void main(java.lang.String[] argv)
The main method
Parameters:
argv - the command line arguments


Submit a bug or feature
For further API reference and developer documentation, see XML Bulk Loader Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Copyright 2002 Sergey Yakovlev, All Rights Reserved.