vbs类生成xml文件

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

  • 有两文件:
    objxml.asp:测试文件
    clsxml.asp:vbs类文件
    代码:
    objxml.asp

    <%@ language=vbscript %>
    <% option explicit %>
    <!--#include file="clsxml.asp"-->
    <%
    dim objxml, strpath, str
    set objxml = new clsxml

    strpath = server.mappath(".") !amp; "\new.xml"

    objxml.createfile strpath, "root"
    'or if using an existing xml file&#58
    'objxml.file = "c:\file.xml"

    objxml.createrootchild "images"

    'here only one attribute is added to the images/image node
    objxml.createchildnodewattr "images", "image", "id", "1"
    objxml.updatefield "images/image[@id=1]", "super.gif"
    objxml.createrootnodewattr "jobs", array("size", "length", "width"), _
    array(24, 31, 30)
    objxml.createrootnodewattr "jobs", array("size", "length", "width"), _
    array(24, 30, 29)
    objxml.createrootnodewattr "jobs", array("size", "length", "width"), _
    array(24, 31, 85)

    'notice that all three job nodes have size 24, all of those
    'nodes will be updated
    objxml.updatefield "jobs[@size=24]", "24's"

    'notice that only two nodes have the specified xpath, hence
    'only two new child nodes will be added
    objxml.createchildnodewattr "jobs[@size=24 and @length=31]", "specs", _
    array("wood", "metal", "color"), _
    array("cedar", "aluminum", "green")

    'it is always important to iterate through all of the nodes
    'returned by this xpath query.
    for each str in objxml.getfield("jobs[@size=24]")
    response.write(str !amp; "
    ")
    next
    set objxml = nothing

    response.redirect "new.xml"
    %>

    clsxml.asp:

    <%
    class clsxml
    'strfile must be full path to document, ie c:\xml\xmlfile.xml
    'objdoc is the xml object
    private strfile, objdoc

    '*********************************************************************
    ' initialization/termination
    '*********************************************************************

    'initialize class members
    private sub class_initialize()
    strfile = ""
    end sub

    'terminate and unload all created objects
    private sub class_terminate()
    set objdoc = nothing
    end sub

    '*********************************************************************
    ' properties
    '*********************************************************************

    'set xml file and objdoc
    public property let file(str)
    set objdoc = server.createobject("microsoft.xmldom")
    objdoc.async = false
    strfile = str
    objdoc.load strfile
    end property

    'get xml file
    public property get file()
    file = strfile
    end property

    '*********************************************************************
    ' functions
    '*********************************************************************

    'create blank xml file, set current obj file to newly created file
    public function createfile(strpath, strroot)
    dim objfso, objtextfile
    set objfso = server.createobject("scripting.filesystemobject")
    set objtextfile = objfso.createtextfile(strpath, true)
    objtextfile.writeline("<@xml version=""1.0""@>")
    objtextfile.writeline("<" !amp; strroot !amp; "/>")
    objtextfile.close
    me.file = strpath
    set objtextfile = nothing
    set objfso = nothing
    end function

    'get xml field(s) based on xpath input from root node
    public function getfield(strxpath)
    dim objnodelist, arrresponse(), i
    set objnodelist = objdoc.documentelement.selectnodes(strxpath)
    redim arrresponse(objnodelist.length)
    for i = 0 to objnodelist.length - 1
    arrresponse(i) = objnodelist.item(i).text
    next
    getfield = arrresponse
    end function

    'update existing node(s) based on xpath specs
    public function updatefield(strxpath, strdata)
    dim objfield
    for each objfield in objdoc.documentelement.selectnodes(strxpath)
    objfield.text = strdata
    next
    objdoc.save strfile
    set objfield = nothing
    updatefield = true
    end function

    'create node directly under root
    public function createrootchild(strnode)
    dim objchild
    set objchild = objdoc.createnode(1, strnode, "")
    objdoc.documentelement.appendchild(objchild)
    objdoc.save strfile
    set objchild = nothing
    end function

    'create a child node under root node with attributes
    public function createrootnodewattr(strnode, attr, val)
    dim objchild, objattr
    set objchild = objdoc.createnode(1, strnode, "")
    if isarray(attr) and isarray(val) then
    if ubound(attr)-lbound(attr) <> ubound(val)-lbound(val) then
    exit function
    else
    dim i
    for i = lbound(attr) to ubound(attr)
    set objattr = objdoc.createattribute(attr(i))
    objchild.setattribute attr(i), val(i)
    next
    end if
    else
    set objattr = objdoc.createattribute(attr)
    objchild.setattribute attr, val
    end if
    objdoc.documentelement.appendchild(objchild)
    objdoc.save strfile
    set objchild = nothing
    end function

    'create a child node under the specified xpath node
    public function createchildnode(strxpath, strnode)
    dim objparent, objchild
    for each objparent in objdoc.documentelement.selectnodes(strxpath)
    set objchild = objdoc.createnode(1, strnode, "")
    objparent.appendchild(objchild)
    next
    objdoc.save strfile
    set objparent = nothing
    set objchild = nothing
    end function

    'create a child node(s) under the specified xpath node with attributes
    public function createchildnodewattr(strxpath, strnode, attr, val)
    dim objparent, objchild, objattr
    for each objparent in objdoc.documentelement.selectnodes(strxpath)
    set objchild = objdoc.createnode(1, strnode, "")
    if isarray(attr) and isarray(val) then
    if ubound(attr)-lbound(attr) <> ubound(val)-lbound(val) then
    exit function
    else
    dim i
    for i = lbound(attr) to ubound(attr)
    set objattr = objdoc.createattribute(attr(i))
    objchild.setattribute attr(i), val(i)
    next
    end if
    else
    set objattr = objdoc.createattribute(attr)
    objchild.setattribute attr, val
    end if
    objparent.appendchild(objchild)
    next
    objdoc.save strfile
    set objparent = nothing
    set objchild = nothing
    end function

    'delete the node specified by the xpath
    public function deletenode(strxpath)
    dim objold
    for each objold in objdoc.documentelement.selectnodes(strxpath)
    objdoc.documentelement.removechild objold
    next
    objdoc.save strfile
    set objold = nothing
    end function
    end class
    %>

  • 上一篇:在web界面下如何生成像资源管理器一样的树形目录
  • 下一篇:xml轻松学习手册(5)xml语法之四
  • 最后更新时间:2024年12月22日
  • 返回页面顶端
ppdesk