Solved

Import XML into recordset *NOT PERSIST FORMAT*

Posted on 2004-08-11
5
1,292 Views
Last Modified: 2007-12-19
I'm looking for some sample code that will allow me to convert an XML file into an ADO recordset.  Excel seems to have functions built in that convert XML straight into a table format in exactly the style I need, but it seems clumsy to have to open the XML file in Excel, save that down and then open that as a recordset - surely there is an easier way?

PLEASE NOTE this XML file was not created using the XML Persist option from the Save method of ADO.  It is just a bog standard XML file:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Devices>
<Device>
<Device_Name />
<LC_Description>LC GBR</LC_Description>
<Site_ID>WLH</Site_ID>
<Device_ID>10005999</Device_ID>
</Device>
<Device>
<Device_Name />
<LC_Description>LC GBR</LC_Description>
<Site_ID>WLH</Site_ID>
<Device_ID>10006005</Device_ID>
</Device>
</Devices>

Can anyone help?

I'll need some sample code to get the points I'm afraid, pointing me in the right direction won't be enough.  A complete answer will get a further 500 points in a separately posted question.
0
Comment
Question by:Belazir
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:arif_eqbal
Comment Utility
Well it can be done easily if you use ADO 2.5 Library, it allows you to create a ADO recordset from an XML file opened as a straem.

You want sample code, a full blown sample is available visit:

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q263/2/47.ASP&NoWebContent=1

OR

search for string

"How To Obtain an ADO Recordset from XML"

in MSDN
0
 

Author Comment

by:Belazir
Comment Utility
Arif

From MSDN:

"For more information regarding the schema an XML document must conform to to be opened correctly by an ADO 2.5 recordset, see the "XML Persistence Format" section of the following article:
XML Integration Features in ADO 2.5"

As stated, my XML file does not conform so I cannot open it this way.  If this is possible without the format being as described please show me how as I have tried this with no success.
B

0
 
LVL 19

Expert Comment

by:arif_eqbal
Comment Utility
Well If this does not work then I think then you will need to parse the xml file using some xml parser and create a disconnected recordset manually.
0
 
LVL 1

Accepted Solution

by:
LeighWardle earned 500 total points
Comment Utility
Try this code:

Sub import_xml_to_recordset(strDBFilePath As String, strTableName As String, strXMLFileName As String, strXMLParentNodeName As String)

Dim MyDB As Database, Fd() As New Field

Dim rsMaterialTypes As Recordset

ReDim Fd(1 To 3)

Const DB_DOUBLE = 7  ' Set field type constant.
Set MyDB = OpenDatabase(strDBFilePath)   ' Open database.
Set rsMaterialTypes = MyDB.OpenRecordset(strTableName, dbOpenDynaset)

    Dim oDom As MSXML2.DOMDocument
    Dim oRoot As MSXML2.IXMLDOMNode

    Set oDom = New MSXML2.DOMDocument
    If Not oDom.Load(strXMLFileName) Then
        Debug.Print "XML ParseError:"
        Debug.Print oDom.parseError.reason
    Else
        Set oRoot = oDom.documentElement

        Dim oItems As MSXML2.IXMLDOMNodeList
        Dim oItem As MSXML2.IXMLDOMNode
        Dim oChildNodes As MSXML2.IXMLDOMNodeList
        Dim oChildNode As MSXML2.IXMLDOMNode
        Set oItems = oRoot.selectNodes(strXMLParentNodeName)
       
        Debug.Print "found records in XML file:" & oItems.length
       
       
        For Each oItem In oItems
       
                'Debug.Print "number of child nodes:" & oItem.childNodes.length
               
               
       
            Debug.Print
            Debug.Print "MaterialTypes_ImportTest:"
           
               
                rsMaterialTypes.AddNew
               
               
                intFieldNo = 0
                For Each oChildNode In oItem.childNodes
               
                    Debug.Print vbTab & oChildNode.nodeName & "=" & oChildNode.Text


                    rsMaterialTypes(oChildNode.nodeName) = oChildNode.Text
                   
                Next oChildNode
       
                rsMaterialTypes.Update
               
        Next oItem

    End If
    Set oRoot = Nothing
    Set oDom = Nothing

End Sub

Regards, Leigh
0
 

Author Comment

by:Belazir
Comment Utility
Thanks Leigh, just what I was after.
B
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now