mozdev.org

JSLib
Page Updated: January 16, 2004
Latest Version: 0.1.375
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 Date

File: utils/sax.js

Module Identifier:
jslib_sax

Fuction List

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();
var h = new testDocumentHandler();
gParser.setDocumentHandler(h);

while (!done) {
    // get data somehow
    gParser.parse(data);
}
Here is an exmple of using this parser within a web page.
<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>





The jslib project can be contacted through the mailing list or the member list.
Copyright © 2000-2017. All rights reserved. Terms of Use & Privacy Policy.