MS Access VBA 解析 XML 文件
Posted
技术标签:
【中文标题】MS Access VBA 解析 XML 文件【英文标题】:MS Access VBA parse XML File 【发布时间】:2015-04-08 20:48:24 【问题描述】:我在这里过得很艰难。我正在尝试使用 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 才等于文件的最后一行。我做错了什么?
【问题讨论】:
能否提供XML结构和示例数据? 【参考方案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)"
【讨论】:
以上是关于MS Access VBA 解析 XML 文件的主要内容,如果未能解决你的问题,请参考以下文章
按下按钮后在 MS Access 中导入 .DBF 文件,使用 VBA