您的位置:首页 > 编程语言 > ASP

使用 Asp 创建 XML 文件

2006-06-08 13:31 459 查看
<!--#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")

%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息