关闭

使用 Asp 创建 XML 文件

标签: xmlaspsqlfunctionlistencoding
1313人阅读 评论(0) 收藏 举报

<!--#include file="conn.asp"-->
<%
Function LoadFile(ByVal File)
    Dim objStream
    On Error Resume Next
    Set objStream = Server.CreateObject("ADODB.Stream")
    If Err.Number=-2147221005 Then
        Response.Write "<div align='center'>非常遗憾,您的主机不支持ADODB.Stream,不能使用本程序</div>"
        Err.Clear
        Response.End
    End If
    With objStream
        .Type = 1
        .Open
        .LoadFromFile Server.MapPath(File)
        If Err.Number<>0 Then
    Response.Write "<div align='center'>文件<font color='#ff0000'>"&File&"</font>无法被打开,请检查是否存在!</font></div>"
    Err.Clear
    Response.End
        End If
        LoadFile = .Read()
        .Close
    End With
    Set objStream = Nothing
End Function
%>
<%
'创建DOM对象
set objDom=server.CreateObject("MicroSoft.XMLDom")

'取得xml数据
'方法1 取得xml文件的xml数据
'objDom.load("c:/test.xml")
'方法2 取得xml数据串的数据
objDom.loadxml("<?xml version=""1.0"" encoding=""utf-8"" ?><Document/>")

'创建一个节点对象
Set Newnode = objDom.DocumentElement'objDom.CreateElement("Document")
Set NewnodeChild=objDom.CreateElement("PostDateTime")
NewnodeChild.Text = now
Newnode.appendChild NewnodeChild
Set NewnodeChild=objDom.CreateElement("TableName")
NewnodeChild.Text = "demo"
Newnode.appendChild NewnodeChild
Set NewnodeChild=objDom.CreateElement("KeyField")
NewnodeChild.Text = "ID"
Newnode.appendChild NewnodeChild
Set NewnodeChild=objDom.CreateElement("DataTable")
'数据表描述
Set ScNodeChild = objDom.CreateElement("Field")
ScNodeChild.Text = "ID"
Set NewAttribute=objDom.CreateNode("attribute","Lable","")
NewAttribute.Text = "ID"
ScNodeChild.SetAttributeNode NewAttribute
Set NewAttribute=objDom.CreateNode("attribute","DataType","")
NewAttribute.Text = "int"
ScNodeChild.SetAttributeNode NewAttribute
NewnodeChild.appendChild ScNodeChild

Set ScNodeChild = objDom.CreateElement("Field")
ScNodeChild.Text = "Title"
Set NewAttribute=objDom.CreateNode("attribute","Lable","")
NewAttribute.Text = "标题"
ScNodeChild.SetAttributeNode NewAttribute
Set NewAttribute=objDom.CreateNode("attribute","DataType","")
NewAttribute.Text = "varchar"
ScNodeChild.SetAttributeNode NewAttribute
Set NewAttribute=objDom.CreateNode("attribute","Len","")
NewAttribute.Text = "200"
ScNodeChild.SetAttributeNode NewAttribute

Set ScNodeChild = objDom.CreateElement("Field")
ScNodeChild.Text = "Content"
Set NewAttribute=objDom.CreateNode("attribute","Lable","")
NewAttribute.Text = "内容"
ScNodeChild.SetAttributeNode NewAttribute
Set NewAttribute=objDom.CreateNode("attribute","DataType","")
NewAttribute.Text = "text"
ScNodeChild.SetAttributeNode NewAttribute
NewnodeChild.appendChild ScNodeChild

Set ScNodeChild = objDom.CreateElement("Field")
ScNodeChild.Text = "PostDateTime"
Set NewAttribute=objDom.CreateNode("attribute","Lable","")
NewAttribute.Text = "发布时间"
ScNodeChild.SetAttributeNode NewAttribute
Set NewAttribute=objDom.CreateNode("attribute","DataType","")
NewAttribute.Text = "datetime"
ScNodeChild.SetAttributeNode NewAttribute
NewnodeChild.appendChild ScNodeChild

Set ScNodeChild = objDom.CreateElement("Field")
ScNodeChild.Text = "AddFile"
Set NewAttribute=objDom.CreateNode("attribute","Lable","")
NewAttribute.Text = "附件"
ScNodeChild.SetAttributeNode NewAttribute
Set NewAttribute=objDom.CreateNode("attribute","DataType","")
NewAttribute.Text = "text"
ScNodeChild.SetAttributeNode NewAttribute
NewnodeChild.appendChild ScNodeChild

Newnode.appendChild NewnodeChild
'END 数据表描述
'新增数据述
Set NewnodeChild=objDom.CreateElement("Add")
'END 新增数据述
 Sql = "select * from Demo where Status='新增'"
 rs.open Sql,conn,1,3
 do while not rs.eof
  Set ScNodeChild = objDom.CreateElement("List")
  Set TrNodeChild = objDom.CreateElement("ID")
  TrNodeChild.Text = rs("ID") 
  ScNodeChild.appendChild TrNodeChild
  Set TrNodeChild = objDom.CreateElement("Title")
  TrNodeChild.Text = Server.HtmlEncode(rs("Title"))
  ScNodeChild.appendChild TrNodeChild
  Set TrNodeChild = objDom.CreateElement("Content")
  TrNodeChild.Text = Server.HtmlEncode(rs("Content"))
  ScNodeChild.appendChild TrNodeChild
  Set TrNodeChild = objDom.CreateElement("PostDateTime")
  TrNodeChild.Text = rs("PostDateTime")
  ScNodeChild.appendChild TrNodeChild
  Set TrNodeChild = objDom.CreateElement("AddFile")
  if rs("AddFile") <> "" then
   Set NewAttribute=objDom.CreateNode("attribute","FileType","")
   NewAttribute.Text = rs("FileType")
   TrNodeChild.SetAttributeNode NewAttribute
   TrNodeChild.datatype = "bin.base64"
   TrNodeChild.nodeTypedvalue = LoadFile(rs("AddFile"))
  end if
  ScNodeChild.appendChild TrNodeChild
  NewnodeChild.appendChild ScNodeChild
 rs.movenext
 loop
 rs.close
 Newnode.appendChild NewnodeChild
'修改数据
 Set NewnodeChild=objDom.CreateElement("Edit")
 Sql = "select * from Demo where Status='修改'"
 rs.open Sql,conn,1,3
 do while not rs.eof
  Set ScNodeChild = objDom.CreateElement("List")
  Set TrNodeChild = objDom.CreateElement("KeyValue")  
  TrNodeChild.Text = rs("id")
  ScNodeChild.appendChild TrNodeChild
  Set FourNodeChild = objDom.CreateElement("Field")
  Set NewAttribute=objDom.CreateNode("attribute","FieldName","")
  NewAttribute.Text = "Title"
  FourNodeChild.SetAttributeNode NewAttribute    
  FourNodeChild.Text = Server.HtmlEncode(rs("Title")) 
  ScNodeChild.appendChild FourNodeChild   
  Set FourNodeChild = objDom.CreateElement("Field")
  Set NewAttribute=objDom.CreateNode("attribute","FieldName","")
  NewAttribute.Text = "Content"
  FourNodeChild.SetAttributeNode NewAttribute  
  FourNodeChild.Text = Server.HtmlEncode(rs("Content"))  
  ScNodeChild.appendChild FourNodeChild
  Set FourNodeChild = objDom.CreateElement("Field")
  Set NewAttribute=objDom.CreateNode("attribute","FieldName","")
  NewAttribute.Text = "AddFile"
  FourNodeChild.SetAttributeNode NewAttribute  
  if rs("AddFile") <> "" then
   Set NewAttribute=objDom.CreateNode("attribute","FileType","")
   NewAttribute.Text = rs("FileType")
   FourNodeChild.SetAttributeNode NewAttribute
   FourNodeChild.datatype = "bin.base64"
   FourNodeChild.nodeTypedvalue = LoadFile(rs("AddFile"))
  end if  
  ScNodeChild.appendChild FourNodeChild   
  NewnodeChild.appendChild ScNodeChild
 rs.movenext
 loop
 rs.close
 Newnode.appendChild NewnodeChild
'End 修改数据
'删除数据
 Set NewnodeChild=objDom.CreateElement("Delete")
 Set ScNodeChild = objDom.CreateElement("List")
 Sql = "select * from Demo where Status='删除'"
 rs.open Sql,conn,1,3
 do while not rs.eof
  Set TrNodeChild = objDom.CreateElement("KeyValue")  
  TrNodeChild.Text = rs("id")
  ScNodeChild.appendChild TrNodeChild  
 rs.movenext
 loop
 rs.close
 NewnodeChild.appendChild ScNodeChild
 Newnode.appendChild NewnodeChild
'End 删除数据
'objDom.appendChild Newnode
objDom.save(Server.MapPath("demo.xml"))
connClose 
Response.ContentType="application/xml"
Response.Write(objDom.xml)
'Response.Redirect("demo.xml")

%>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:168929次
    • 积分:2997
    • 等级:
    • 排名:第12132名
    • 原创:126篇
    • 转载:3篇
    • 译文:0篇
    • 评论:28条
    文章分类
    最新评论