快捷搜索:

ADO数据与XML数据间的转换的类

当对现稀有据库的数据进行阐发时,常常必要对某一部分的数据进行阐发.此时,应用

1.SQL查询阐发器?

但其每每不直不雅,查找某个关键字又必要从新履行新的SQL.

2.SQLXML模板?

但又不必然有权限建立新的虚拟目录,且某些SQL语句SQLXML模板不支持

数据拆离时也有相似问题。

尤其当不合收集,不合情况,必要从新导入数据,进行阐发或拆离,艰苦尤为显着。

能不能有一种措施,可以将数据离开于数据库进行阐发,必要时再导入到数据库中?

XML是个很好的选择!

ADO本身支持数据到XML的转换,只必要对其款式进行解析,成为自己的XML文件通用款式,就可以进行本地阐发

而对通用XML款式进行数据库映射,就可完成数据从新导入数据库的事情.

下面是一个ADO数据(表的基础数据)与XML数据间的互相转换的类(ASP实现),初步完成表数据的导入、导出。

通用表间关系映射(经由过程XSD描述),斟酌之中,盼望各位见示辅导,不胜感激.

一个调用类的例子:

example.asp

0) Then' nErrCode(差错代码)为0,运行成功

Response.Write oXMLData.GetErrExegesis(oXMLData.nErrCode)

'nErrCode(差错代码),经由过程措施GetErrExegesis() 得到注释

End IF

End Sub

Sub Import()'// 导入数据

oXMLData.sXMLFile= "Data.xml" ' 必须数据源XML文件(包孕相对路径)

oXMLData.sVacancyCols = "nLabelID" ' 必须指定某些字段的值可以不导入(樊篱字段)

' 款式"nID,dDate"(以‘,’分隔字段)

oXMLData.Import (oDbConn)

IF (oXMLData.nErrCode=0) Then

Response.Write "数据导入成功!"

Else

Response.Write oXMLData.GetErrExegesis(oXMLData.nErrCode)

End IF

End Sub

%>

类的代码:

TransformData.asp

0 Then

m_nErrCode = m_nErrCode_XMLDOM

Exit Sub

End If

sXSLStr= GetXSL()

m_oXMLDOM.async = false

m_oXSLDOM.async = false

m_oXSLDOM.loadxml(sXSLStr)

sDataXML = ""

sDataXML = sDataXML & ""

nMaxI = Ubound(m_aSQlData, 1)

For nI=0 To nMaxI

sTableName = m_aSQlData(nI, 0)

If (Len(sTableName) > 0) Then

sSQL= m_aSQlData(nI, 1)

sXMLStr= GetDataXML(sTableName, sSQL, p_oDbConn)

IF (m_nErrCode > m_nErrCode_NotErr) Then

Exit Sub

End IF

sDataXML = sDataXML & sXMLStr

End If

Next

sDataXML = sDataXML & ""

IF (m_bIsOutput) Then

Call ResponseXML(sDataXML)

End IF

IF (m_bIsSave) Then

Call SaveDataXML(sDataXML)

End IF

End Sub

'奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫**

' 函数: GetRndFileName()

' 描述: 得到随机名称,由当前光阴和7位随机数字构成

'奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫**

Private Function GetRndFileName()

Dim nMax, nMin

Dim sRnd, sDate

Randomize

nMin = 1000000

nMax = 9999999

sRnd = Int( ( (nMax - nMin + 1) * Rnd ) + nMin)

sDate = WordStr( WordStr( WordStr( now(), "-", "") , ":", ""), " ", "")

GetRndFileName = "_" & sDate & sRnd & ".xml"

End Function

'奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫**

' 函数: GetXSL()

' 描述: 得到XSL文件字符串

'奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫**

Private Function GetXSL()

Dim sXSLStr

sXSLStr = ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & "select='@*'>"

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

sXSLStr = sXSLStr & ""

GetXSL = sXSLStr

End Function

'奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫**

' 函数: GetDataXML(ByRef p_sTableName, ByRef p_sSQL, ByRef p_oDbConn)

' 描述: 履行单条SQL,得到数据转换后的XML

' 参数:

'1.p_sTableName : 表的名称

'2.p_sSQL: 读取数据的SQl语句

'3.p_oDbConn: 数据库连接工具

'

'奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫**

Private Function GetDataXML(ByRef p_sTableName, ByRef p_sSQL, ByRef p_oDbConn)

Dim oRecordset

Dim sXMLStr, sCleanXML

Dim nEnsData

ON ERROR RESUME NEXT

nEnsData= 0

Set oRecordset = p_oDbConn.Execute(p_sSQL)

If Err.Number "

sXMLStr= sXMLStr & sCleanXML

sXMLStr= sXMLStr & ""

Else

sXMLStr= ""

End IF

GetDataXML = sXMLStr

End Function

'奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫**

' 历程: SaveDataXML(ByRef p_sXMLStr)

' 描述: 保存XML款式的字符串到文件

' 参数:

'p_sXMLStr : XML款式的字符串

'奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫**

Private Sub SaveDataXML(ByRef p_sXMLStr)

Dim sFileInfo

If (Len(m_sSaveFileName) = 0) Then

m_sSaveFileName = GetRndFileName()

End If

If (Len(m_sSaveFilePath) = 0) Then

sFileInfo = m_sSaveFileName

Else

IF (Right(m_sSaveFilePath,1) = "/")Then

sFileInfo = m_sSaveFilePath & m_sSaveFileName

Else

sFileInfo = m_sSaveFilePath & "/" & m_sSaveFileName

End IF

End If

m_oXMLDOM.loadxml(p_sXMLStr)

ON ERROR RESUME NEXT

m_oXMLDOM.save ( Server.MapPath(sFileInfo) )

If Err.Number 0 Then

m_nErrCode = m_nErrCode_XMLDOM

Exit Sub

End If

m_oXMLDOM.async = false

m_oXMLDOM.load( Server.MapPath(m_sXMLFile) )

If Err.Number 0 Then

m_nErrCode = m_nErrCode_WriteData

Exit Sub

End If

End Sub

'奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫**

' 函数: GetImportSQL(ByRef p_oDataBase)

' 描述: 得到将XML数据转换为SQL后的字符串

' 参数:

'p_oDataBase: XML文件的根节点

'

'奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫**

Private Function GetImportSQL(ByRef p_oDataBase)

Dim oTable, oRow, oDatas, oData

Dim sColNames, sColValues

Dim sColName

Dim sSQL

sSQL = ""

For Each oTable In p_oDataBase.childNodes

For Each oRow In oTable.childNodes

Set oDatas = oRow.selectNodes("@*")

sColNames = ""

sColValues = ""

For Each oData In oDatas

sColName = oData.nodeName

If ( Instr( Lcase(Cstr(m_sVacancyCols)), Lcase(Cstr("," & sColName & ",")) )

您可能还会对下面的文章感兴趣: