欢迎光临
我们一直在努力

An ASP Class for XML Data Transfer(vbs类生成xml文件),希望对大家有用

建站超值云服务器,限时71元/月

有两文件:
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(".") & "\new.xml"

objxml.createfile strpath, "root"
or if using an existing xml file:
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]", "24s"

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 & "<br>")
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("<" & strroot & "/>")
    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
%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » An ASP Class for XML Data Transfer(vbs类生成xml文件),希望对大家有用
分享到: 更多 (0)

相关推荐

  • 暂无文章