Results 1 to 8 of 8
  1. #1
    Meichmann is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2012
    Location
    New Jersy USA
    Posts
    12

    Import Specific XML elements into variable using VBA

    Hello Everyone,
    I have a troubling issue and I hope you can help me. I have an xml document which stores information about an address:

    <ResourceSets>
    -<ResourceSet>
    <EstimatedTotal>1</EstimatedTotal>
    -<Resources>
    -<Location>
    <Name>Address Here</Name>
    -<Point>
    <Latitude>Numerical Latitude</Latitude>
    <Longitude>Numerical Longitude</Longitude>
    </Point>


    <Location>
    </Resources>
    </ResourceSet>
    </ResourceSets>

    The acutal xml file was edit to only show what i need. What i am looking to do is extract the "Latitude" and "Longitude" text values and place them in a variable using VBA. I am just unsure of the drilldown into the child nodes and sub child nodes to get it.

    Thank you!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Did you try import to a table?

    You can open the xml file as a text file object in VBA. Read each line and test for the words Latitude and Longitude in the string and parse the numeric values.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Meichmann is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2012
    Location
    New Jersy USA
    Posts
    12
    Quote Originally Posted by June7 View Post
    Did you try import to a table?

    You can open the xml file as a text file object in VBA. Read each line and test for the words Latitude and Longitude in the string and parse the numeric values.
    yes i did. everytime i go to import the xml file it creates new tables, it doesnt overwrite the data in the previous tables. this xml file will be constantly rewritten because of queries.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Those are the only options I am aware of.

    You want the previous table overwritten? Code can delete the table then the new import take its place.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    To add to June's comments...

    You can save the import procedure and then call the procedure using VBA.
    DoCmd.RunSavedImportExport "SavedProcedureName"

    You can use the following to delete a table within the current DB
    DoCmd.DeleteObject acTable, "TableName"

    If you want to append a table with an XML file, you can import the XML file and then import the same file a second time. On the second import, select the option to append existing tables and then save THAT procedure with a meaningful name. Since you imported the same file twice, you will need to delete some records in your permanent table.

  6. #6
    Meichmann is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2012
    Location
    New Jersy USA
    Posts
    12
    You can load the xml file into memory using the "xmlDoc.Load (Name)" command, so saving the data to a table is not necessary. I just need the code to get to the "Latitude" and "Longitude" nodes and extract the values.

    I have this code:

    Set NodeList = xmlDoc.getElementsByTagName("Response")

    For Each iNode In NodeList
    For Each iAtt In iNode.Attributes
    MsgBox iAtt.Name & ": " & iAtt.nodeTypedValue
    Next

    This code returns all the attributes in the "response" node, which is the first node in the file right below the declaration. The response node was omitted because it just holds metadata.
    What i need is to drill down to the "Point" node and extract the values of the siblings.

    Importing data into a table is not an option because of permission settings in the application.

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You will have to nest your loops as you enumerate each parent. Maybe something like this. You could possibly go directly to the child node with the following code but, maybe the parent nodes have info you need. So tthis should iterate through Response and Point. Test it out and see....

    Code:
    Dim xmlDoc As New DOMDocument60
    Dim selNodeList As IXMLDOMSelection
    Dim iNode As IXMLDOMNode
    Dim selNodePoint As IXMLDOMSelection
    Dim iPoint As IXMLDOMNode
    Dim elmLat As IXMLDOMElement
    Dim elmLon As IXMLDOMElement
    Dim intResponse As Integer
    intResponse = 0
    xmlDoc.Load "C:\Test\Sample.xml"
        Set selNodeList = xmlDoc.getElementsByTagName("Response")
        
        For Each iNode In selNodeList
        Debug.Print "Response Node Index = " & intResponse
        
            Set selNodePoint = xmlDoc.getElementsByTagName("Point")
            
            For Each iPoint In selNodePoint
                
                Set elmLat = iPoint.childNodes(0)
                Set elmLon = iPoint.childNodes(1)
            Debug.Print "Point Node Value = " & elmLat.nodeTypedValue & "Lat " & elmLon.nodeTypedValue & " Lon"
            Next    'iPont
        
        intResponse = intResponse + 1
        Next    'iNode

  8. #8
    Meichmann is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2012
    Location
    New Jersy USA
    Posts
    12
    I was able to figure it out. here is the code:


    Dim xmlDoc As New DOMDocument
    Dim iNode As MSXML2.IXMLDOMNode
    Dim iNode2 As MSXML2.IXMLDOMNode
    Dim NodeList As IXMLDOMNodeList
    Dim strAddress As String
    Dim strCoordinates As String
    strAddress = UrlAddress

    xmlDoc.async = False
    xmlDoc.Load strAddress
    xmlDoc.Save ("c:\Access\Geocode.xml")
    xmlDoc.Load ("c:\Access\Geocode.xml")

    'If xmlDoc.parseError.ErrorCode <> 0 Then
    ' MsgBox "Invalid XML, Load Failed"
    ' Else
    ' MsgBox "Xml Loaded Successfully"
    'End If

    Set NodeList = xmlDoc.SelectNodes("//Response/ResourceSets/ResourceSet/Resources/Location/Point")
    For Each iNode In NodeList
    For Each iNode2 In iNode.ChildNodes
    If iNode2.nodeName = "Latitude" Then
    strCoordinates = iNode2.nodeTypedValue
    End If

    If iNode2.nodeName = "Longitude" Then
    strCoordinates = strCoordinates & "," & iNode2.nodeTypedValue
    End If

    Next iNode2
    Next iNode

    I recieved the message that you replied ItsMe after i figured it out. but thanks for replying!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2013, 10:12 AM
  2. Replies: 6
    Last Post: 03-13-2013, 03:03 PM
  3. Replies: 8
    Last Post: 12-05-2012, 07:24 PM
  4. Replies: 5
    Last Post: 08-22-2012, 04:48 PM
  5. Replies: 3
    Last Post: 06-02-2011, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums