将sax加入我们的asp应用中

  • 作者:未知 来源:未知 添加时间:2006年7月3日 字体:

  •     在处理大型的xml文档的时候,在服务器端直接使用xml dom的时候速度是比较慢的,当我第一次接触到sax的时候,我意识到我们应该在服务器端将dom和sax结合起来以提高我们程序的效率。我们在asp中最常使用的就是使用com来完成这个工作(也许你有更好的方法)。

    废话少说,还是直接进入实际的代码部分了(下面只是包含了最基本的代码而已)。



    首先我们创建一个dll来封装sax的功能好了。

    测试环境:win2k prof.+msxml3.0 sp1+vb6

    要使用sax我们必须引用(reference)microsoft xml 3.0(我的机器安装的是msxml3.0 sp1)



    工程名:saxtesting



    类名:clssaxtest

    方法:public function myxmlparser(xml文件物理路径) as domdocument

    代码:

    option explicit





    public function myxmlparser(byval strxmlfilepath as variant) as domdocument

    dim reader as new saxxmlreader

    dim contenthandler as new contenthandlerimpl

    dim errorhandler as new errorhandlerimpl





    set reader.contenthandler = contenthandler

    set reader.errorhandler = errorhandler

    on error goto errorhandle

    dim xmlfile as string

    xmlfile = strxmlfilepath

    reader.parseurl (xmlfile)



    dim xmldoc as msxml2.domdocument

    set xmldoc = createobject("msxml2.domdocument")

    xmldoc.loadxml strxml

    set myxmlparser = xmldoc

    set xmldoc = nothing

    exit function



    errorhandle:

    err.raise 9999, "my xml parser", err.number !amp; " : " !amp; err.description



    end function



    类名:modpublic

    代码:

    option explicit



    global strxml as string



    类名:contenthandlerimpl

    代码:

    option explicit



    implements ivbsaxcontenthandler





    private sub ivbsaxcontenthandler_startelement(strnamespaceuri as string, strlocalname as string,



    strqname as string, byval attributes as msxml2.ivbsaxattributes)



    dim i as integer



    intlocker = intlocker + 1

    if intlocker > 1 then



    end if

    strxml = strxml !amp; "<" !amp; strlocalname



    for i = 0 to (attributes.length - 1)

    strxml = strxml !amp; " " !amp; attributes.getlocalname(i) !amp; "=""" !amp; attributes.get&#118alue(i) !amp;



    """"

    next



    strxml = strxml !amp; ">"



    if strlocalname = "qu" then

    err.raise vbobjecterror + 1, "contenthandler.startelement", "found element <qu>"

    end if



    end sub



    private sub ivbsaxcontenthandler_endelement(strnamespaceuri as string, strlocalname as string,



    strqname as string)



    strxml = strxml !amp; "</" !amp; strlocalname !amp; ">"



    end sub



    private sub ivbsaxcontenthandler_characters(text as string)

    text = replace(text, vblf, vbcrlf)

    strxml = strxml !amp; text



    end sub



    private property set ivbsaxcontenthandler_documentlocator(byval rhs as msxml2.ivbsaxlocator)



    end property



    private sub ivbsaxcontenthandler_enddocument()



    end sub



    private sub ivbsaxcontenthandler_endprefixmapping(strprefix as string)



    end sub



    private sub ivbsaxcontenthandler_ignorablewhitespace(strchars as string)



    end sub



    private sub ivbsaxcontenthandler_processinginstruction(target as string, data as string)



    strxml = strxml !amp; "<@" !amp; target !amp; " " !amp; data !amp; ">"



    end sub



    private sub ivbsaxcontenthandler_skippedentity(strname as string)



    end sub



    private sub ivbsaxcontenthandler_startdocument()



    end sub



    private sub ivbsaxcontenthandler_startprefixmapping(strprefix as string, struri as string)



    end sub





    类名:errorhandlerimpl

    代码:

    option explicit



    implements ivbsaxerrorhandler





    private sub ivbsaxerrorhandler_fatalerror(byval lctr as ivbsaxlocator, msg as string, byval



    errcode as long)



    strxml = strxml !amp; "*** error *** " !amp; msg



    end sub



    private sub ivbsaxerrorhandler_error(byval lctr as ivbsaxlocator, msg as string, byval errcode as



    long)



    end sub



    private sub ivbsaxerrorhandler_ignorablewarning(byval olocator as msxml2.ivbsaxlocator,



    strerrormessage as string, byval nerrorcode as long)



    end sub





    ok,让我们编译这个dll,应该是没什么问题了。让我们在asp中看看运行的结果怎么样:

    xml文件:

    <@xml version="1.0"@>

    <root foo="bar.com">

    <parts>

    <part foo="bar.com" foo2="bar.com">

    <partno>12345</partno>

    <description>vip - very important part</description>

    </part>

    <part>

    <partno>5678</partno>

    <description>lip - less important part</description>

    </part>

    </parts>

    </root>



    asp文件:

    <%

    set a = createobject("saxtesting.clssaxtest")

    set xmldoc = a.myxmlparser("d:\test.xml")

    response.contenttype="text/xml"

    response.write xmldoc.xml

    set xmldoc=nothing

    set a=nothing

    %>

  • 上一篇:xml schema学习笔记
  • 下一篇:掌握sax
  • 最后更新时间:2024年12月22日
  • 返回页面顶端
ppdesk