I have a requirement that my program should navigate between XML data source and SQL server. I have to read records from the XML file and have to return ADORecordset
.
Here is the rough piece of code (Form1
):
if (optionDB) then
GetDBRecords 'this function should return recordset
else
GetXMLRecords ' this function should return Recordset
end if
Module1: ' this module contains code related to DB
Module2: ' This module should contain code related to XML
Public Function getXmlRecords() As ADODB.Recordset
Dim oXMLDom As New DOMDocument
Dim Recordset As New ADODB.Recordset
If oXMLDom.Load(App.Path + "\data.xml") = False Then
MsgBox "Failed to load xml开发者_如何学编程 data from file."
End If
Set Recordset = RecordsetFromXMLDocument(oXMLDom)
End Function
Public Function RecordsetFromXMLDocument(XMLDOMDocument As DOMDocument) As Recordset
Dim oRecordset As ADODB.Recordset
Set oRecordset = New ADODB.Recordset
oRecordset.Open XMLDOMDocument ' pass the DOM Document instance as the Source argument
Set RecordsetFromXMLDocument = oRecordset ' return the recordset
Set oRecordset = Nothing
End Function
It is throwing me this error:
Recordset cannot be created.Source XML is incomplete or invalid.
What should i add to the XML document? I am new to these concepts.
By default xml is loaded asynchronously.
Set oXMLDom.async = False
before loading.
And obviously, the XML must be in a form understandable by ADODB. That is, it must use certain namespaces and have data in certain format. Example:
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly' rs:updatable='true'>
<s:AttributeType name='foo' rs:number='1' rs:write='true'>
<s:datatype dt:type='int' dt:maxLength='4' rs:precision='0' rs:fixedlength='true' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='bar' rs:number='2' rs:write='true'>
<s:datatype dt:type='string' dt:maxLength='255' rs:precision='0' rs:maybenull='false'/>
</s:AttributeType>
<s:extends type='rs:rowbase'/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row foo='1' bar='one'/>
<z:row foo='2' bar='two'/>
<z:row foo='3' bar='three'/>
</rs:data>
</xml>
You can omit s:Schema
section and only include rs:data
, if you already have your recordset structured with required fields.
If you mean to feed just any random XML to ADODB, you better first transform it with XSL.
I've not tried this myself but the following article looks interesting:
Microsoft OLE DB Simple Provider
Simple providers are intended to access data sources that require only fundamental OLE DB support, such as in-memory arrays or XML documents... The OLE DB Simple Provider (OSP) in MDAC 2.7 or later has been enhanced to support opening hierarchical ADO Recordsets over arbitrary XML files.
精彩评论