|
Page Updated: January 15, 2004
Latest Version: 0.1.359 |
|
Debug |
Install | File
I/O |
RDF |
Network |
Sound | Utils |
XUL | Zip
|
|
|
Home | Mailing List |
Install |
Source Code |
Downloads | Bugs
| docs
| clients |
Sax parser
Status: Up to DateFile: utils/sax.js
Module Identifier: jslib_sax
Fuction List
- Constructor
- SAXParser ()
- Creates a parser.
- Visible members (the only ones you should use).
- setDocumentHandler (aHandler)
- This sets a handler for the parser. This is basicly
setting
the callback function for the tags you parse.
- parse (aData)
- Feed data to this function to parse it.
Instructions
To use the parser you would first create a handler in Javascript to determine what you want out of the parsed information.This is an example that just prints (the the page) what tages are being parsed.
function testDocumentHandler()
{
this.open = false;
this.empty = false;
}
testDocumentHandler.prototype.startDocument = function()
{
this.open = true;
document.write("startDocument()\n");
}
testDocumentHandler.prototype.endDocument = function()
{
this.open = false;
document.write("\nendDocument()\n");
}
testDocumentHandler.prototype.startElement = function(name, attribs)
{
if (this.empty)
{
document.write(">");
}
document.write("<" + name);
for (attrib in attribs)
{
document.write(" " + attrib + "='" + attribs[attrib] + "'");
}
this.empty = true;
}
testDocumentHandler.prototype.endElement = function(name)
{
if (this.empty)
{
document.write("/>");
} else {
document.write("</" + name + ">");
}
this.empty = false;
}
testDocumentHandler.prototype.characters = function(data)
{
if (this.empty)
{
document.write(">");
this.empty = false;
}
document.write(data);
}
Then in your Javascript code, you need to create an instance of the parser and attach the handler.
gParser = new SAXParser();Here is an exmple of using this parser within a web page.
var h = new testDocumentHandler();
gParser.setDocumentHandler(h);
while (!done) {
// get data somehow
gParser.parse(data);
}
<head>
<title>Simple SAX Parser Test</title>
<script src="sax.js"></script>
<script src="saxtest.js"></script>
</head>
<body>
<h2>Simple SAX Parser Test</h2>
<pre>
<script>
p = new SAXParser();
p.setDocumentHandler(new testDocumentHandler());
p.parse("<test foo='bar' xmlns='http://foo.org/bar.dtd'>\n");
p.parse("<iq type='set' id='auth'><query xmlns='jabber:iq:auth'><username>B</username><password>b</password><resource>one</resource></query></iq>\n");
p.parse("blah <some> sample other <x m='l'/> and <fun/> with </some> <e>l<e/>m</e>nts\n");
p.parse("</test>");
</script>
</pre>
</body>