MS Access VBA解析XML文件

时间:2015-04-08 20:48:24

标签: xml vba access-vba ms-access-2013

我在这里度过了难关。我正在尝试使用VBA导入和解析XML文件。但是,.LoadXML函数似乎不起作用。我已加载Microsoft XML,v6.0引用,XML文件有效。这是代码的一部分:

Public Function ParseXML(ByVal strXMLFilename As String) As Boolean

Dim intFile As Integer
Dim strXMLFile As String
Dim xmlDoc As Object
Dim xmlTransmission As Object 'MSXML2.IXMLDOMNode
Dim xmlSurvey As Object 'MSXML2.IXMLDOMNode
Dim xmlRecord As Object 'MSXML2.IXMLDOMNode
Dim xmlField As Object 'MSXML2.IXMLDOMNode
Dim xmlAttrib As MSXML2.IXMLDOMAttribute
Dim ctrFields As Long
Dim strTargetTable As String
Dim xmlrs As DAO.Recordset

Set xmlDoc = New MSXML2.DOMDocument

intFile = FreeFile()
Open strXMLFilename For Input As intFile
strXMLFile = input$(LOF(1), 1)
Close intFile

Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0")
xmlDoc.LoadXML strXMLFile


ctrFields = 0

For Each xmlTransmission In xmlDoc.ChildNodes 'xmlNodes
    '...do a bunch of stuff...
Next xmlTransmission

End Function

一旦进入“For Each”语句,它就跳出循环退出函数。我还尝试使用:

加载strXMLFile变量
Open strXMLFilename For Input As intFile
Line Input #intFile, strXMLFile
Close intFile

但是只有当我到达.LoadXML函数时,strXMLFile才等于文件的最后一行。我做错了什么?

1 个答案:

答案 0 :(得分:1)

.loadXML Method从字符串加载XML,但如果您已经有文件规范,则可以使用.load Method直接从文件加载XML。例如,给定以下文件" sample.gpx" (导航设备用于存储位置的XML文件)...

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" creator="Extra_POI_Editor V5.33" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd">
  <metadata><!--
                                                                                                                                                                                                                                                       Extra_POI_Editor can only be used for personnal usage and not for any commercial purposes without a written permission of the author, aka TurboCCC. I can be reached at turboccc@hotmail.com.
  --><link href="http://turboccc.wikispaces.com"></link>
    <time>2013-10-26T00:36:17Z</time>
  </metadata>

  <wpt lat="51.056128" lon="-113.982223">
    <name>Harveys: Calgary</name>
    <cmt> </cmt>
    <extensions>
      <gpxx:WaypointExtension>
        <gpxx:Address>
          <gpxx:StreetAddress>Northgate Village Mall, 525-36th St. N.E.</gpxx:StreetAddress>
          <gpxx:City>Calgary</gpxx:City>
          <gpxx:State>AB</gpxx:State>
          <gpxx:PostalCode>T2A 6K3</gpxx:PostalCode>
        </gpxx:Address>
        <gpxx:PhoneNumber>403-272-9641</gpxx:PhoneNumber>
      </gpxx:WaypointExtension>
    </extensions>
  </wpt>

  <wpt lat="51.134358" lon="-114.010802">
    <name>Harveys: Calgary (Airport)</name>
    <cmt> </cmt>
    <extensions>
      <gpxx:WaypointExtension>
        <gpxx:Address>
          <gpxx:StreetAddress>2000 Airport Rd. N.E.</gpxx:StreetAddress>
          <gpxx:City>Calgary</gpxx:City>
          <gpxx:State>AB</gpxx:State>
          <gpxx:PostalCode>T2E 6W5</gpxx:PostalCode>
        </gpxx:Address>
        <gpxx:PhoneNumber>403-250-9177</gpxx:PhoneNumber>
      </gpxx:WaypointExtension>
    </extensions>
  </wpt>

</gpx>

...以下代码将提取每个位置的经度,纬度和名称......

Option Compare Database
Option Explicit

Sub xmlTest()
    Dim lon As Double, lat As Double, poiName As String
    ' VBA project reference required:
    ' Microsoft XML, v6.0
    Dim xmlDoc As New MSXML2.DOMDocument60
    If xmlDoc.Load("C:\__tmp\sample.gpx") Then
        Dim xmlTopLevelNode As MSXML2.IXMLDOMNode
        For Each xmlTopLevelNode In xmlDoc.childNodes
            If xmlTopLevelNode.nodeName = "gpx" Then
                Dim gpxNode As MSXML2.IXMLDOMNode
                For Each gpxNode In xmlTopLevelNode.childNodes
                    If gpxNode.nodeName = "wpt" Then
                        Dim wptAttribute As MSXML2.IXMLDOMNode
                        For Each wptAttribute In gpxNode.Attributes
                            Select Case wptAttribute.nodeName
                                Case "lat":
                                    lat = CDbl(wptAttribute.nodeTypedValue)
                                Case "lon":
                                    lon = CDbl(wptAttribute.nodeTypedValue)
                            End Select
                        Next
                        Set wptAttribute = Nothing
                        Dim wptChildNode As MSXML2.IXMLDOMNode
                        For Each wptChildNode In gpxNode.childNodes
                            If wptChildNode.nodeName = "name" Then
                                poiName = wptChildNode.nodeTypedValue
                            End If
                            Exit For
                        Next
                        Set wptChildNode = Nothing
                        Debug.Print lon & "," & lat & "," & """" & Replace(poiName, """", """""") & """"
                    End If
                Next
                Set gpxNode = Nothing
            End If
        Next
        Set xmlTopLevelNode = Nothing
    Else
        Debug.Print "Unable to load XML file."
    End If
    Set xmlDoc = Nothing
End Sub

...并将其打印到VBA立即窗口:

-113.982223,51.056128,"Harveys: Calgary"
-114.010802,51.134358,"Harveys: Calgary (Airport)"