HTML 4.01 strict HTML
Visit Sourceforge.net


CONTENTS
What files do I need to include to use the SAX Parser?
How do I get started with the SAX Parser?
What SAX Events and Interfaces are supported?
Is there a pre-built event handler object available?
Do I need to catch every event fired by the SAX Parser?
What's this handleCharacterData function for?
 
SAXDriver Object - Constructor
SAXDriver Object - parse method
SAXDriver Object - setDocumentHandler method
SAXDriver Object - setErrorHandler method
SAXDriver Object - setLexicalHandler method
 
SAXEventHandler Object - Constructor
SAXEventHandler Object - characters event
SAXEventHandler Object - comment event
SAXEventHandler Object - endCDATA event
SAXEventHandler Object - endDocument event
SAXEventHandler Object - endElement event
SAXEventHandler Object - error event
SAXEventHandler Object - fatalError event
SAXEventHandler Object - processingInstruction event
SAXEventHandler Object - setDocumentLocator event
SAXEventHandler Object - startCDATA event
SAXEventHandler Object - startDocument event
SAXEventHandler Object - startElement event
SAXEventHandler Object - warning event
SAXEventHandler Object - _handleCharacterData method
SAXEventHandler Object - _fullCharacterDataReceived
 
trim
What files do I need to include to use the SAX Parser?

To use XML for <SCRIPT>'s SAX Parser, you need to include xmlsax.js from the jsXMLParser directory.

If you wish to use a compressed version of the parser (comments, line breaks etc removed) you may replace xmlsax.js with tinyxmlsax.js from the jsXMLParser/compressed directory.

How do I get started with the SAX Parser?

It's actually very easy. XML for <SCRIPT> ships with a pre-built SAXEventHandler object that is ready to accept events from the SAXDriver object. Simply copy the code in preMadeSaxEventHandler.js (located in the jsXMLParser directory) into your code (or link to the file) and write code to handle events as you see fit.

For example, if you wanted to show the user an alert box with the name of each element as it was parsed, you would write the following code:

function startParser(xml) {
var parser = new SAXDriver();
var eventHandler = new SAXEventHandler();
parser.setDocumentHandler(eventHandler);
parser.setLexicalHandler(eventHandler);
parser.setErrorHandler(eventHandler);
parser.parse(xml);
}; // end function startParser

The code to show the alert box at the start of each element would reside in the startElement event sink and would look like the following:

SAXEventHandler.prototype.startElement = function(name, atts) {
alert(name);
} // end function startElement
What SAX Events and Interfaces are supported?

The SAX "standard" defines the following interfaces. See below for the support offerred by XML for <SCRIPT>

Implemented Interfaces:
//Document Interface
SAXDocumentHandler.startDocument ()
SAXDocumentHandler.endDocument ()
SAXDocumentHandler.startElement (name, atts)
SAXDocumentHandler.endElement (name)
SAXDocumentHandler.characters (data, start, length)
SAXDocumentHandler.processingInstruction (target, data)
SAXDocumentHandler.setDocumentLocator (locator)

//Lexical Interface
SAXLexicalHandler.startCDATA ()
SAXLexicalHandler.endCDATA ()
SAXLexicalHandler.comment (data, start, length)

//Error Interface
SAXErrorHandler.warning (exception)
SAXErrorHandler.error (exception)
SAXErrorHandler.fatalError (exception)

Unimplemented Interfaces:
SAXDocumentHandler.ignorableWhitespace (data, start, length)
SAXLexicalHandler.startDTD (name, publicId, systemId)
SAXLexicalHandler.endDTD ()
SAXLexicalHandler.startEntity (name)
SAXLexicalHandler.endEntity (name)
Is there a pre-built event handler object available?

Yes! Please see the file preMadeSaxEventHandler.js in the jsXMLParser directory. This pre-built object supports all three event handling interfaces (Document, Lexical and Error) as well as provides built in methods for correctly handling character data.

Please keep in mind that you don't *have* to use this pre-built object if you don't want to. You may certainly roll your own if it suits you needs better.

Do I need to catch every event fired by the SAX Parser?

No. XML for <SCRIPT>'s SAX Driver object is capable of detecting whether or not an event handler exists for the event it wants to fire. If no event handler exists, the event will not fire and the parsing will continue.

What's this handleCharacterData function for?

During the parsing of an XML document, it is possible that the characters event may fire multiple times for a single element. If you place your characters event handling code in this event, you may not get all of the character data you expect.

To compensate for this behavior, XML for <SCRIPT>'s pre-built event handler includes a function called _handleCharacterData. This function is designed to capture all of the character data reported to the event handler and report it to the user all at once in the _fullCharacterDataReceived function. This reporting is done only when the function is sure there is no more character data expected for that element.

For more information on the _handleCharacterData function, click here

For more information on the _fullCharacterDataReceived function, click here

SAXDriver Object - Constructor

var saxParser = new SAXDriver();

Accepts:
N/A

Returns:
new SAXDriver object

Example:
function parseXML(xml) {
//instantiate the SAX Driver object
var saxParser = new SAXDriver();

//instantiate the pre-built event handler object
var eventHandler = new SAXEventHandler();

//tell the SAX driver object where to send events
saxParser.setDocumentHandler(eventHandler);
saxParser.setLexicalHandler(eventHandler);
saxParser.setErrorHandler(eventHandler);

//start the parsing process
saxParser.parse(xml);
}
SAXDriver Object - parse method

saxParser.parse(<xml>);

Accepts:
<xml> - String containing the XML to parse

Returns:
N/A

The parse method begins the process of parsing the XML data. The processing will complete before the next line of code is called.

Example:
function parseXML(xml) {
//instantiate the SAX Driver object
var saxParser = new SAXDriver();

//instantiate the pre-built event handler object
var eventHandler = new SAXEventHandler();

//tell the SAX driver object where to send events
saxParser.setDocumentHandler(eventHandler);
saxParser.setLexicalHandler(eventHandler);
saxParser.setErrorHandler(eventHandler);

//start the parsing process
saxParser.parse(xml);
}
SAXDriver Object - setDocumentHandler method

saxParser.setDocumentHandler(<Handler Object>);

Accepts:
<Handler Object> - An object that supports the Document Event Interface

Returns:
N/A

setDocumentHandler tells the SAX parser what code to call for the following events:

startDocument ()
endDocument ()
startElement (name, atts)
endElement (name)
characters (data, start, length)
processingInstruction (target, data)
setDocumentLocator (locator)

Your handler object does not need to support all of these events. Events that do not interest you can be left out with no ill-effect. However, for the events that you do wish to trap, your handler object must implement the above functions exactly as shown.

For your convenience, XML for <SCRIPT> provides a pre-built event handler in the file preMadeSaxEventHandler.js under the jsXMLParser directory. This handler implements all three SAX interfacs for you so you do not have to code them yourself.

Example:
function parseXML(xml) {
//instantiate the SAX Driver object
var saxParser = new SAXDriver();

//instantiate the pre-built event handler object
var eventHandler = new SAXEventHandler();

//tell the SAX driver object where to send events
saxParser.setDocumentHandler(eventHandler);
saxParser.setLexicalHandler(eventHandler);
saxParser.setErrorHandler(eventHandler);

//start the parsing process
saxParser.parse(xml);
}
SAXDriver Object - setErrorHandler method

saxParser.setErrorHandler(<Handler Object>);

Accepts:
<Handler Object> - An object that supports the Error Event Interface

Returns:
N/A

setErrorHandler tells the SAX parser what code to call for the following events:

SAXErrorHandler.warning (exception)
SAXErrorHandler.error (exception)
SAXErrorHandler.fatalError (exception)

Your handler object does not need to support all of these events. Events that do not interest you can be left out with no ill-effect. However, for the events that you do wish to trap, your handler object must implement the above functions exactly as shown.

For your convenience, XML for <SCRIPT> provides a pre-built event handler in the file preMadeSaxEventHandler.js under the jsXMLParser directory. This handler implements all three SAX interfacs for you so you do not have to code them yourself.

Example:
function parseXML(xml) {
//instantiate the SAX Driver object
var saxParser = new SAXDriver();

//instantiate the pre-built event handler object
var eventHandler = new SAXEventHandler();

//tell the SAX driver object where to send events
saxParser.setDocumentHandler(eventHandler);
saxParser.setLexicalHandler(eventHandler);
saxParser.setErrorHandler(eventHandler);

//start the parsing process
saxParser.parse(xml);
}
SAXDriver Object - setLexicalHandler method

saxParser.setLexicalHandler(<Handler Object>);

Accepts:
<Handler Object> - An object that supports the Lexical Event Interface

Returns:
N/A

setLexicalHandler tells the SAX parser what code to call for the following events:

SAXLexicalHandler.startCDATA ()
SAXLexicalHandler.endCDATA ()
SAXLexicalHandler.comment (data, start, length)

Your handler object does not need to support all of these events. Events that do not interest you can be left out with no ill-effect. However, for the events that you do wish to trap, your handler object must implement the above functions exactly as shown.

For your convenience, XML for <SCRIPT> provides a pre-built event handler in the file preMadeSaxEventHandler.js under the jsXMLParser directory. This handler implements all three SAX interfacs for you so you do not have to code them yourself.

Example:
function parseXML(xml) {
//instantiate the SAX Driver object
var saxParser = new SAXDriver();

//instantiate the pre-built event handler object
var eventHandler = new SAXEventHandler();

//tell the SAX driver object where to send events
saxParser.setDocumentHandler(eventHandler);
saxParser.setLexicalHandler(eventHandler);
saxParser.setErrorHandler(eventHandler);

//start the parsing process
saxParser.parse(xml);
}
SAXEventHandler Object - Constructor

var eventHandler = new SAXEventHandler();

Accepts:
N/A

Returns:
New SAXEvent handler object

The SAXEventHandler object is XML for <SCRIPT>'s pre-built event handler. It exists for your convenience. If you choose to roll your own event handler, you may ignore the information contained in the constructor section of the documentation. If you do roll your own event handler, it will need to have the same function signatures as the pre-built event handler for the events you will be trapping.

Example:
function parseXML(xml) {
//instantiate the SAX Driver object
var saxParser = new SAXDriver();

//instantiate the pre-built event handler object
var eventHandler = new SAXEventHandler();

//tell the SAX driver object where to send events
saxParser.setDocumentHandler(eventHandler);
saxParser.setLexicalHandler(eventHandler);
saxParser.setErrorHandler(eventHandler);

//start the parsing process
saxParser.parse(xml);
}
SAXEventHandler Object - characters event

characters(data, start, length)

Provides:
data - the raw xml data provided to the parser
start - position to start getting characters
length - number of characters to get

During the parsing of an XML document, it is possible that the characters event may fire multiple times for a single element. If you place your characters event handling code in this event, you may not get all of the character data you expect.

To compensate for this behavior, XML for <SCRIPT>'s pre-built event handler includes a function called _handleCharacterData. This function is designed to capture all of the character data reported to the event handler and report it to the user all at once in the _fullCharacterDataReceived function. This reporting is done only when the function is sure there is no more character data expected for that element.

The characters event traps the character data being reported by the parser, and appends the new data to an internal variable that keeps track of all the previous character data that had been reported for the current element.

For more information on the _handleCharacterData function, click here

For more information on the _fullCharacterDataReceived function, click here

Example:
characters (data, start, length) {
this.characterData += data.substr(start, length);
}
SAXEventHandler Object - comment event

comment(data, start, length)

Provides:
data - the raw xml data provided to the parser
start - position to start getting characters
length - number of characters to get

The comment event is fired whenever comment data is found in the XML stream. For example, the following XML will yield the events listed below if XML for <SCRIPT>'s pre-built event handler is used.

NOTE: Only the event name is reported here. For a full description of the events fired with this XML, please see testsComment1() in saxTestSuite.js

NOTE: If the "characters" event is listed below, it is assumed to come from the function _fullCharacterDataReceived.

XML:
<?xml version="1.0"?>
<root>
<!--Comment Text-->
</root>

Events:
setDocumentLocator
startDocument
processingInstruction
startElement
comment
endElement
endDocument

Example:
comments (data, start, length) {
var commentData = data.substr(start, length);
}
SAXEventHandler Object - endCDATA event

endCDATA()

Provides:
N/A

The endCDATA event is fired whenever the end of a CDATA section is found in the XML stream. For example, the following XML will yield the events listed below if XML for <SCRIPT>'s pre-built event handler is used.

NOTE: Only the event name is reported here. For a full description of the events fired with this XML, please see testsCDATA1() in saxTestSuite.js.

NOTE: If the "characters" event is listed below, it is assumed to come from the function _fullCharacterDataReceived.

XML:
<?xml version="1.0"?>
<root>
<![CDATA[CDATA Text]]>
</root>

Events:
setDocumentLocator
startDocument
processingInstruction
startElement
startCDATA
characters
endCDATA
endElement
endDocument

Example:
endCDATA() {
//make note that we have reached end of CDATA tag
}
SAXEventHandler Object - endDocument event

endDocument()

Provides:
N/A

The endDocument event is fired whenever the end of the document is found in the XML stream. For example, the following XML will yield the events listed below if XML for <SCRIPT>'s pre-built event handler is used.

NOTE: Only the event name is reported here. For a full description of the events fired with this XML, please see testsElement1() in saxTestSuite.js

NOTE: If the "characters" event is listed below, it is assumed to come from the function _fullCharacterDataReceived.

XML:
<?xml version="1.0"?>
<root>
</root>

Events:
setDocumentLocator
startDocument
processingInstruction
startElement
endElement
endDocument

Example:
endDocument() {
//make note that we have reached end of the document
}
SAXEventHandler Object - endElement event

endElement(<Element Name>)

Provides:
<Element Name> - the name of the element that is ending

The endElement event is fired whenever the end of an element is found in the XML stream. For example, the following XML will yield the events listed below if XML for <SCRIPT>'s pre-built event handler is used.

NOTE: Only the event name is reported here. For a full description of the events fired with this XML, please see testsElement1() in saxTestSuite.js

NOTE: If the "characters" event is listed below, it is assumed to come from the function _fullCharacterDataReceived.

XML:
<?xml version="1.0"?>
<root>
</root>

Events:
setDocumentLocator
startDocument
processingInstruction
startElement
endElement
endDocument

Example:
endElement(name) {
alert("I have reached the end of the element " + name);
}
SAXEventHandler Object - error event

error(<Exception object>)

Provides:
<Exception object> - Object containing information about the exception

The exception object has these methods:

exception.getMessage() - returns an error string
exception.getLineNumber() - returns the line number of the error
exception.getColumnNumber() - returns the column number of the error

At this time, XML for <SCRIPT>'s SAX parser does not throw this event. All error reporting is provided via the fatalError event. For more information on that event, please click here In the future, this event may be utilized so it is highly recommended that you place error trapping code in your handler for this event.

Example:
error(exception) {
//tell the user there is a problem
alert(exception.getMessage());
}
SAXEventHandler Object - fatalError event

fatalError(<Exception object>)

Provides:
<Exception object> - Object containing information about the exception

The exception object has these methods:

exception.getMessage() - returns an error string
exception.getLineNumber() - returns the line number of the error
exception.getColumnNumber() - returns the column number of the error

The fatalError event is fired whenever an error is found in the XML stream. For example, the following XML will yield the events listed below if XML for <SCRIPT>'s pre-built event handler is used.

NOTE: Only the event name is reported here. For a full description of the events fired with this XML, please see testsElementError1() in saxTestSuite.js

NOTE: If the "characters" event is listed below, it is assumed to come from the function _fullCharacterDataReceived.

XML:
<?xml version="1.0"?>
<root

Events:
setDocumentLocator
startDocument
processingInstruction
fatalError

Example:
fatalError(exception) {
//tell the user there is a problem
alert(exception.getMessage());
}
SAXEventHandler Object - processingInstruction event

processingInstructiont(<Target>, <Data>)

Provides:
<Target> - the target of the processing instruction
<Data> - the data of the processing instruction

The processingInstruction event is fired whenever a processing instruction is found in the XML stream. For example, the following XML will yield the events listed below if XML for <SCRIPT>'s pre-built event handler is used.

NOTE: Only the event name is reported here. For a full description of the events fired with this XML, please see testsPI1() in saxTestSuite.js

NOTE: If the "characters" event is listed below, it is assumed to come from the function _fullCharacterDataReceived.

XML:
<?xml version="1.0"?>
<root>
</root>

Events:
setDocumentLocator
startDocument
processingInstruction
startElement
endElement
endDocument

In the above example, the Target variable passed in would be set to xml while the data variable would be set to version="1.0"

Example:
processingInstructiont(target, data) {
alert("Processing instruction target:" + target);
alert("Processing instruction data:" + data);
}
SAXEventHandler Object - setDocumentLocator event

setDocumentLocator(<Parser Object>)

Provides:
<Parser Object> - A handle to the low level XML Parser

The setDocumentLocator event is always the first event fired when parsing an XML stream. The object provided is a handle to the actuall object behind the scenes responsible for the parsing of the XML. In most cases, you will not need to do anything with this object and this event can safely be ignored. However, if you do need raw access to the underlying parser, it is provided to you here. If you do need to add functionality to this underlying parser, the object that is being returned is defined in xmlsax.js as XMLP.

For example, the following XML will yield the events listed below if XML for <SCRIPT>'s pre-built event handler is used.

NOTE: Only the event name is reported here. For a full description of the events fired with this XML, please see testsElement1() in saxTestSuite.js

NOTE: If the "characters" event is listed below, it is assumed to come from the function _fullCharacterDataReceived.

XML:
<?xml version="1.0"?>
<root>
</root>

Events:
setDocumentLocator
startDocument
processingInstruction
startElement
endElement
endDocument

Example:
setDocumentLocator(ParserObject) {
//ignore this event
}
SAXEventHandler Object - startCDATA event

startCDATA()

Provides:
N/A

The startCDATA event is fired whenever the start of a CDATA section is found in the XML stream. For example, the following XML will yield the events listed below if XML for <SCRIPT>'s pre-built event handler is used.

NOTE: Only the event name is reported here. For a full description of the events fired with this XML, please see testsCDATA1() in saxTestSuite.js.

NOTE: If the "characters" event is listed below, it is assumed to come from the function _fullCharacterDataReceived.

XML:
<?xml version="1.0"?>
<root>
<![CDATA[CDATA Text]]>
</root>

Events:
setDocumentLocator
startDocument
processingInstruction
startElement
startCDATA
characters
endCDATA
endElement
endDocument

Example:
startCDATA() {
//make note that we have found the start of a CDATA tag
}
SAXEventHandler Object - startDocument event

startDocument()

Provides:
N/A

The startDocument event is fired when the start of a the document is found in the XML stream. For example, the following XML will yield the events listed below if XML for <SCRIPT>'s pre-built event handler is used.

NOTE: Only the event name is reported here. For a full description of the events fired with this XML, please see testsElement1() in saxTestSuite.js

NOTE: If the "characters" event is listed below, it is assumed to come from the function _fullCharacterDataReceived.

XML:
<?xml version="1.0"?>
<root>
</root>

Events:
setDocumentLocator
startDocument
processingInstruction
startElement
endElement
endDocument

Example:

Example:
startDocument() {
//make note that we have found the start of the document
}
SAXEventHandler Object - startElement event

startElement(<Name>, <Attributes Object>)

Provides:
<Name> - The name of the element that is starting
<Attribute Object> - An object representing the element's attributes

The Attributes Object (atts) has these methods:

atts.getName(<ordinal>) - gets the name of the attribute specified
atts.getValue(<ordinal>) - gets the value of the attribute specified
atts.getLength() -- gets the number of attributes for the element
atts.getValueByName(<name>) -- gets the value for the attribute specified

The value expected for the methods getName and getValue is a zero based ordinal based on the position of the attribute you're looking for in the attribute list.

The startElement event is fired when the start of an element is found in the XML stream. For example, the following XML will yield the events listed below if XML for <SCRIPT>'s pre-built event handler is used.

NOTE: Only the event name is reported here. For a full description of the events fired with this XML, please see testsElement1() in saxTestSuite.js

NOTE: If the "characters" event is listed below, it is assumed to come from the function _fullCharacterDataReceived.

XML:
<?xml version="1.0"?>
<root>
<tag att1="val1" att2="val2">
</tag>
</root>

Events:
setDocumentLocator
startDocument
processingInstruction
startElement
endElement
endDocument

Example:
startElement(name, atts) {
//get element name
var elm = "Element Name: " + name + "\n");

//now loop through attributes
var intCount = atts.getLength();
if (intCount > 0) {
elm += "Has element data of:\n";
}
for (intLoop=0;intLoop<intCount;intLoop++) {
elm += atts.getName(intLoop) + ": ";
elm += atts.getValue(intLoop) + "\n"
}

//display element info
alert(elm);
}
SAXEventHandler Object - warning event

warning(<Exception object>)

Provides:
<Exception object> - Object containing information about the exception

The exception object has these methods:

exception.getMessage() - returns an error string
exception.getLineNumber() - returns the line number of the error
exception.getColumnNumber() - returns the column number of the error

At this time, XML for <SCRIPT>'s SAX parser does not throw this event. All error reporting is provided via the fatalError event. For more information on that event, please click here In the future, this event may be utilized so it is highly recommended that you place error trapping code in your handler for this event.

Example:
warning(exception) {
//tell the user there is a problem
alert(exception.getMessage());
}
SAXEventHandler Object - _handleCharacterData method

_handleCharacterData()

Provides:
N/A

During the parsing of an XML document, it is possible that the characters event may fire multiple times for a single element. If you place your characters event handling code in this event, you may not get all of the character data you expect.

To compensate for this behavior, XML for <SCRIPT>'s pre-built event handler includes a function called _handleCharacterData. Since the characters event can be fired multiple times for an element, the only way to be sure you have received all of the characters for an element is to wait until an event other than characters is fired while at the same time storing all of the data reported by the multiple characters events. Once this non-character event is fired, then you can be assured you have the complete character data for the element.

_handleCharacterData is the internal function that serves the purpose of checking for non-characters events. It is called at the beginning every event which can signify that all of the characters data has been received. When _handleCharacterEvents is called, it checks the internal variable that holds the collection of characters data. If that varable is non-blank, then the function calls the interal event _fullCharacterDataReceived and resets the internal variable. _fullCharacterDataReceived is where you should put your character handling code.

Generally, it will not be necessary for you to modify this function.

For more information on the _fullCharacterDataReceived function, click here

SAXEventHandler Object - _fullCharacterDataReceived

_fullCharacterDataReceived(fullCharacterData)

Provides:
fullCharacterData - all of the character data for the element

During the parsing of an XML document, it is possible that the characters event may fire multiple times for a single element. If you place your characters event handling code in this event, you may not get all of the character data you expect.

XML for <SCRIPT>'s pre-built event handler compensates for this behavior for you. Once it is sure that all of the character data has been received for an element, it fires _fullCharacterDataReceived. This event is where you should put your characters event handling code.

Example:
_fullCharacterDataReceived(fullCharacterData) {
//show the user all of the character data
alert("characters for this element: " + fullCharacterData);
}
Convenience Functions - trim function

trim(<string>, |trim left true|false |, |trim right |true|false |)

accepts:
<string>: string to be trimmed
|trim left|: (optional) trim the left side of the string
|trim right|: (optional) trim the right side of the string

returns:
string trimmed as desired.

In the trim function, trim left and trim right default to "true" if not passed in.