高分求救,asp调用数据库内容并生成xml文件

gclwlq 2010-04-03 09:56:48

有一数据库pcfinal.mdb,想调用里面的一新闻表pf_news里pf_news_date ; pf_news_category_name ; pf_news_jj ;pf_news_content;pf_uploadfile这些字段的内容。要求生成的xmlnews.xml文件的如下:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<newsList>
<news>
<date><![CDATA[数据库里的新闻时间]]></date>
<title><![CDATA[数据库里的新闻标题]]></title>
<topic><![CDATA[数据库里的新闻简介]]></topic>
<description><![CDATA[数据库里的新闻内容]]></description>
</news>
</newsList>


请各位朋友帮忙,谢谢!
...全文
227 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
nanyang9 2010-04-06
  • 打赏
  • 举报
回复
知道我为什么给你一个写文件的函数吗? 你再看看我回复的.

Set fso = CreateObject("Scripting.FileSystemObject")
使用FSO显然是不行的.

为什么要在Flash里去看呢? 直接用记事本,用浏览器不能查看吗?
gclwlq 2010-04-03
  • 打赏
  • 举报
回复
我用的这个代码:

<%xmlfile=server.mappath("../xmlNews.xml")
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile(xmlfile,True)
MyFile.WriteLine("<?xml version=""1.0"" encoding=""ISO-8859-1""?>")
MyFile.WriteLine("<newsList>")
%>

<%sql_ad2="select * from pf_news order by pf_news_order desc"
Set rs_ad2= Server.CreateObject("ADODB.Recordset")
rs_ad2.open sql_ad2,conn,1,1
do while not rs_ad2.eof
%>
<%MyFile.WriteLine "<news>"%>
<%MyFile.WriteLine "<date><![CDATA["&rs_ad2("pf_news_date")&"]]></date>"%>
<%MyFile.WriteLine "<title><![CDATA["&rs_ad2("pf_news_name")&"]]></title>"%>
<%MyFile.WriteLine "<topic><![CDATA["&rs_ad2("pf_news_jj")&"]]></topic>"%>
<%MyFile.WriteLine "<description><![CDATA["&rs_ad2("pf_news_content")&"]]></description>"%>
<%MyFile.WriteLine "<source>"&rs_ad2("pf_uploadfile")&"</source>"%>
<%MyFile.WriteLine "<url></url>"%>

<%MyFile.WriteLine "</news>"%>

<%
rs_ad2.movenext
loop
rs_ad2.close
set rs_ad2=nothing
%>


<%MyFile.WriteLine "</newsList>"%>
<%

MyFile.close '关闭fout
set MyFile=nothing '清空fout
set fso=nothing
%>

也生成了xml文件,可是flash调用时显示的全是乱码,除了数字能正常显示
nanyang9 2010-04-03
  • 打赏
  • 举报
回复
不好意思,刚说错了一点, Dreamweaver CS4是识别xml文件中的<?xml version="1.0" encoding="***" ?> 编码信息的. 至于老版本的DW是不是同样识别的就不太清楚了.

总之,你要确保生成的过程没有出错. 4楼给出的代码比较完整
nanyang9 2010-04-03
  • 打赏
  • 举报
回复
这个编码问题涉及到的方面比较多,如果统一编码呢, 可以避免很多地方出现问题.
如果你生成的文件没有出错的话,那么我觉得是你的查看工具有问题.
比如如果你的Dreamweaver设置的打开/新建文件的默认编码是gb2312,那用Dreamweaver来查看带汉字的UTF-8的XML文件,则是乱码.
建议你用IE浏览器来查看XML文件,不仅不会出现编码解释出误,而且还可以报告XML语法格式中的错误.

对于编码问题的相关建议,请参考这个贴子
http://topic.csdn.net/u/20100403/19/eee3bacb-d765-4a56-a313-24721f2fd0cc.html?798404406
gclwlq 2010-04-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 nanyang9 的回复:]
推荐使用 utf-8
[/Quote]

用utf-8生成的是<topic><![CDATA["ŵӻӰѣӵĹæˣȻѡһĵӹ
spirit888 2010-04-03
  • 打赏
  • 举报
回复
sub H2XML (strFile,strContent,oMode)

oMode=CInt(oMode)
Set objStream = Server.CreateObject("ADODB.Stream")
On Error Resume Next
With objStream
.Open
.Charset = "ISO-8859-1"
.Position = objStream.Size
.WriteText = strContent
.SaveToFile server.mappath(strFile),oMode '生成的XML文件名
.Close
End With
Set objStream = Nothing

Select Case Err
Case 424 Response.Write "没有权限或目录错误"
Case Else Response.Write Err.Description

End Select
end sub


dim myrss,rss2
rss2=false


myrss = "<?xml version=""1.0"" encoding=""ISO-8859-1"" ?><newsList><news>"

set rs=server.CreateObject("adodb.recordset")
sql="select * from pf_news order by Id desc"
rs.open sql, conn, 1, 1
if not rs.eof then
rss2=true
do while not rs.eof
myrss = myrss&"<date item_url='"&rs("D_Picture")&"' link='"&Rs("PageUrl")&"' itemtitle='"&FunStrvalue(Rs("Text1"),40)&"'></item>"
<date><![CDATA["&rs("pf_news_date")&"]]></date><title><![CDATA["&rs("pf_news_category_name")&"]]></title><topic><![CDATA["&rs("pf_news_jj")&"]]></topic><description><![CDATA["&rs("pf_news_content")&"]]></description>
rs.movenext
loop
end if

myrss=myrss & "</news></newsList>"

if rss2=true then
H2XML "xmlnews.xml", myrss, 2
end if


开关数据库自己写
nanyang9 2010-04-03
  • 打赏
  • 举报
回复
推荐使用 utf-8
gclwlq 2010-04-03
  • 打赏
  • 举报
回复
用这个ISO-8859-1生成的xml文件全是乱码,哪应该用什么呢?
nanyang9 2010-04-03
  • 打赏
  • 举报
回复
简单的说下, 首先数据库得到结果集
再循环结果集(其实是可以不循环的) 连接成一个输出字符串
最后调用写文件的方法来把这个字符串生成文件.

注意: ISO-8859-1
ISO/IEC 8859-1,又称Latin-1或“西欧语言”,是国际标准化组织内ISO/IEC 8859的第一个8位字符集。它以ASCII为基础,在空置的0xA0-0xFF的范围内,加入192个字母及符号,藉以供使用变音符号的拉丁字母语言使用。
我觉得你的XML使用这个编码会很容易出问题.

最后,给你一个可以控制编码写文件函数
Function WriteFile(filepath,str,fCharset)
Dim adSaveCreateOverWrite,adSaveCreateNotExist
adSaveCreateOverWrite = 2
adSaveCreateNotExist = 1
On Error Resume Next
Set stm = Server.Createobject("Adodb.Stream")
stm.Charset = fCharset
stm.Open
stm.WriteText str
stm.SaveToFile filepath,adSaveCreateOverWrite
If Err.Number<>0 then
Err.Clear
WriteFile=False
Else
WriteFile=True
End If
On Error GoTo 0
End Function
这个函数有返回值,写成功返回True

用例

outstr="123456文件内容789"

If WriteFile(Server.Mpa("abc.txt"),outstr,"utf-8") Then
Response.Write("写入文件成功")
Else
Response.Write("写入文件失败")
End If

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧