如何把sql数据库中的表中的数据写入到xml文件里

如题
---------------------------------------------------------------

 1   
 2class autoXML   
 3Private xmldoc   
 4Private xmlfile   
 5  
 6Sub Class_Initialize()   
 7set xmldoc = Server.CreateObject("Microsoft.XMLDOM")   
 8xmldoc.async = fasle   
 9End Sub   
10  
11Sub Class_Terminate()   
12set xmldoc = Nothing   
13End Sub   
14  
15Public Property Let filePath(str)   
16xmlfile = str   
17End Property   
18  
19Sub getRs(rs)   
20dim xmlhead   
21dim pNode,sNode   
22dim aNode,tNode   
23dim tableName   
24dim rf   
25dim i   
26set xmlhead = xmldoc.createProcessingInstruction("xml","version=""1.0"" encoding=""GB2312""")   
27xmldoc.insertBefore xmlhead,xmldoc.childNodes(0)   
28tableName = split(rs.Source,chr(32))   
29set pNode = xmldoc.CreateElement(tableName(3))   
30xmldoc.appendChild pNode   
31redim aNode(rs.Fields.Count-1)   
32do while not rs.eof   
33set sNode = xmldoc.CreateElement(tableName(3)&"list")   
34pNode.appendChild sNode   
35for i=0 to rs.Fields.Count-1   
36if rs.Fields(i).Name=tableName(3) then   
37tNode = rs.Fields(i).Name&"Child"   
38else   
39tNode = rs.Fields(i).Name   
40end if   
41set aNode(i) = xmldoc.CreateElement(tNode)   
42sNode.appendChild aNode(i)   
43if rs.Fields(i).value<>"" then   
44aNode(i).text = rs.Fields(i).value   
45end if   
46next   
47rs.movenext   
48loop   
49End Sub   
50  
51Function saveFile()   
52on error resume next   
53xmldoc.save xmlfile   
54saveFile = (err.number=0)   
55End Function   
56End Class   

调用,把调用recordset就可以了。

 1   
 2dim conn   
 3set conn = Server.CreateObject("adodb.connection")   
 4conn.open "Provider=Microsoft.jet.oledb.4.0;data source="&Server.MapPath("db.mdb")   
 5dim rs,sql   
 6set rs = Server.CreateObject("adodb.recordset")   
 7sql = "select * from news"   
 8rs.Open sql,conn,3,2   
 9  
10dim ixml   
11dim xf   
12set ixml = new autoXML   
13xf = "test.xml"   
14ixml.filePath = Server.MapPath(xf)   
15ixml.getRs(rs)   
16if ixml.saveFile() then   
17response.write "

<a "="" """="" &="" href="" server.urlencode(xf)="">保存完毕</a>

1"   
2else   
3response.write "保存文件错误"   
4end if   
5set ixml = Nothing   
6rs.close   
7set rs = Nothing   

---------------------------------------------------------------

1.asp

 1   
 2dim strConn,conn,rs,xmldoc,xsldoc   
 3dim strSql   
 4strTableName="test1"   
 5  
 6strConn="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=test;Data Source=SHISM;password="   
 7set conn=server.CreateObject("adodb.connection")   
 8conn.Open strConn   
 9  
10strSql="select * from test1"   
11set rs=server.CreateObject("adodb.recordset")   
12rs.Open strSql,conn,1,1   
13set xmldoc=server.CreateObject("msxml2.domdocument")   
14xmldoc.async=false   
15rs.Save xmldoc,1   
16set xsldoc=server.CreateObject("msxml2.domdocument")   
17xsldoc.async=false   
18xsldoc.load server.MapPath("toXml.xsl")   
19  
20Response.Write xmldoc.transformNode(xsldoc)   
21set xsldoc=nothing   
22set xmldoc=nothing   
23rs.close   
24set rs=nothing   
25set cmd=nothing   

toXML.xsl

 1<xsl:stylesheet version="1.0" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:z="#RowsetSchema">
 2<xsl:output method="xml" omit-xml-declaration="yes"></xsl:output>
 3<xsl:template match="/">
 4<xsl:element name="ROOT">
 5<xsl:apply-templates></xsl:apply-templates>
 6</xsl:element>
 7</xsl:template>
 8<xsl:template match="/xml/rs:data/z:row">
 9<xsl:element name="element">
10<xsl:for-each select="@*">
11<xsl:attribute name="{name()}">
12<xsl:value-of select="."></xsl:value-of>
13</xsl:attribute>
14</xsl:for-each>
15</xsl:element>
16</xsl:template>
17</xsl:stylesheet>
Published At
Categories with Web编程
comments powered by Disqus