Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Import XML into recordset *NOT PERSIST FORMAT*

Posted on 2004-08-11
5
Medium Priority
?
1,337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 11780776
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
ID: 11781114
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
ID: 11781176
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 2000 total points
ID: 11791453
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
ID: 11791528
Thanks Leigh, just what I was after.
B
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

730 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