使用 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")
%>
<%
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")
%>
相关文章推荐
- 使用ASP读取XML文件
- asp.net下创建、查询、修改带名称空间的 XML 文件的例子
- C++使用tinyxml创建和解析xml文件(本文用于cocos2dx)
- 使用.NET读取XML文件,.net开发,.net,ASP.NET
- ASP.NET中根据XML动态创建并使用WEB组件(一)
- 使用 ASP.NET 所创建的 XML Web 服务的设计指南
- 使用myeclispe或者sts工具,创建的myBatis的 xml文件不提示的问题解决方法
- 一个创建XMLHttpRequest对象的js文件(适用asp,php,jsp,asp.net等)
- 使用ASP.NET页面创建可视的UI,在客户机上实例化MSXML分析器的页面从中间层组件取出XML数据、操作并显示 并有问题请高手解决
- 错误:"无法从命名空间绑定的导入"错误消息出现,当您试图通过使用 Web 服务描述语言工具创建 XML Web 服务代理文件时
- 【2】使用DuiLib的XML文件创建界面
- ASP.NET中根据XML动态创建并使用WEB组件(二)
- 使用Asp.net 生成 xml 文件,两种方式(DataSet和自定义模式)
- 使用 ASP.NET 创建的 XML Web 服务的概述
- ASP.NET下使用xml反序列化、缓存依赖实现个性化配置文件的实时生效
- 使用dom4j创建一个xml文件
- 使用ASP读取XML文件
- asp.net下创建、查询、修改带名称空间的 XML 文件的例子
- Spring_使用XML配置文件创建对象
- 使用 ASP.NET 所创建的 XML Web 服务与 XML Web 服务的客户端