欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp下用fso和ado.stream写xml文件的方法

程序员文章站 2022-05-03 12:31:32
asp按关键字查询xml的问题 '------------------------------------------------------ '读取文件 readtxtf...

asp按关键字查询xml的问题
'------------------------------------------------------
'读取文件 readtxtfile(filename)
'------------------------------------------------------
function readtxtfile(filename)
dim fso,f1,ts,filepath
filepath=server.mappath(filename)
set fso = createobject("scripting.filesystemobject")
set ts = fso.opentextfile(filepath,1,1)
readtxtfile = ts.readall
set ts=nothing
set fso=nothing
end function
'------------------------------------------------------------
'把信息写入文件
'------------------------------------------------------------
function writetxtfile(text,filename)
path=server.mappath(filename)
set fso = createobject("scripting.filesystemobject")
set f1 = fso.createtextfile(path,true)
f1.write (text)
f1.close
end function
'-----------------------------------------------------------
'生成xml文件
'-----------------------------------------------------------
msg = "<?xml version=""1.0"" encoding=""utf-8""?>"
msg=msg & "<bcaster>"
msg=msg & "<item item_url=""//www.jb51.net"" itemtitle=""""/>"
msg=msg & "</bcaster>"
call writetxtfile(msg,"x1.xml")


fso默认是ascii编码的,因为必须使用utf-8编码,用ado.stream来写这个文件,代码如下:
sub createfile(text,filename)
dim st
set st=server.createobject("adodb.stream")
st.type=2
st.mode=3
st.charset="utf-8"
st.open()
st.writetext text
st.savetofile server.mappath(filename),2
st.close()
set st=nothing
end sub
msg = "<?xml version=""1.0"" encoding=""utf-8""?>"
msg=msg & "<bcaster>"
msg=msg & "<item item_url=""//www.jb51.net"" itemtitle=""""/>"
msg=msg & "</bcaster>"
call createfile(msg,"x1.xml")