从 WS 导入 XML 并在 VBA 中的访问表中解析
Posted
技术标签:
【中文标题】从 WS 导入 XML 并在 VBA 中的访问表中解析【英文标题】:Importing XML from WS and parsing in access table in VBA 【发布时间】:2018-10-30 19:34:32 【问题描述】:我正在从网络服务导入数据。我成功了,但数据导入了 3 个表。我发现了各种我认为可行的选项,但对于哪个选项最好且最有意义感到困惑。
我发现的选项是
使用 XSLT 转换 XML 多种解析方式,包括XPATH
真的,我已经阅读了很多内容,以至于对我应该做什么感到困惑。
这里是 XML
<ArrayOfVehicle xmlns="http://schemas.datacontract.org/2004/07/Xata.Ignition.WebServiceAPI.Contracts.DataContract.Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Vehicle>
<AllowTrailerInspection>true</AllowTrailerInspection>
<Auxiliary>None</Auxiliary>
<CompanyName>Company</CompanyName>
<CompanySID>1234</CompanySID>
<Country>USA</Country>
<EldVehicle>true</EldVehicle>
<FuelDrawCapacity>0</FuelDrawCapacity>
<GrossVehicleWeight>0</GrossVehicleWeight>
<HP>0</HP>
<HUT>false</HUT>
<HasBerth>false</HasBerth>
<HasElectronicEngine>true</HasElectronicEngine>
<HosExempt>false</HosExempt>
<IFTA>true</IFTA>
<InstallDate>2018-10-01T13:01:00</InstallDate>
<LicensePlate></LicensePlate>
<ManualVIN>false</ManualVIN>
<Manufacture></Manufacture>
<ManufactureDate>1900-01-01T00:00:00</ManufactureDate>
<Model></Model>
<ModifiedBy>331</ModifiedBy>
<ModifiedDate>2018-10-19T20:26:03.648543</ModifiedDate>
<OBCType>ABCRelay</OBCType>
<Odometer>0</Odometer>
<OdometerDate>2018-10-01T04:00:00</OdometerDate>
<OrganizationID>ABCLTL</OrganizationID>
<OrganizationName>ABCLTL</OrganizationName>
<OrganizationSID>32</OrganizationSID>
<OwnerOperator>false</OwnerOperator>
<PowerAxles>1</PowerAxles>
<ResourceGroupIdList xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:string>NOR DISPATCH</a:string>
</ResourceGroupIdList>
<SID>34163</SID>
<StateProvince>Texas</StateProvince>
<Status>Active</Status>
<StraightTruck>false</StraightTruck>
<TGTNumber>123456</TGTNumber>
<TransmissionMfg></TransmissionMfg>
<TransmissionType></TransmissionType>
<Type>Tractor</Type>
<UserDefinedField1></UserDefinedField1>
<UserDefinedField2></UserDefinedField2>
<UserDefinedField3></UserDefinedField3>
<UserDefinedField4></UserDefinedField4>
<UserDefinedField5></UserDefinedField5>
<VIN>1FVAHGFCXAJJR8537</VIN>
<VehicleName>001234SCAC</VehicleName>
<Year></Year>
</Vehicle>
<Vehicle>
我只想导入
<VehicleName>
<TGTNumber> and
<a:string> inside of <ResourceGroupIDList....
这就是我目前在 VBA 中所拥有的
Public Sub UpdateTrucks(strUID As String, strPassword As String)
'Debug.Print strDate
Dim reader As New XMLHTTP60
Dim strUserID As String
Dim strRequest As String
Dim strSQL As String
Dim rs As DAO.Recordset
strSQL = "tblResourceGroups1" 'defines the Table result that you want to loop
Set rs = CurrentDb.OpenRecordset(strSQL)
strUserID = "1234567|" & strUID
strPassword = strPassword
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
While (Not rs.EOF)
strRequest = "http://ws.Website.com/VehicleWebService.svc/vehicles/?ResourceGroupID=" & rs.Fields("ResourceGroupName")
'Debug.Print strRequest
reader.Open "GET", strRequest, rs.Fields("ResourceGroupName"), strUserID, strPassword
reader.send
Do Until reader.ReadyState = 4
DoEvents
Loop
If reader.status = 200 Then
'importXML
'current
Set doc = reader.responseXML
doc.Save "C:\Data\Table.xml"
Application.ImportXML "C:\Data\Table.xml", acStructureAndData
ElseIf reader.status = 401 Then
MsgBox "Unable to authenticate. The username and password do not match with the system."
ElseIf reader.status = 500 Then
MsgBox "Due to an internal issue the system is unable to take the desired request. Please try again later."
End If
rs.MoveNext
Wend
Else
MsgBox "Unable to import data."
End If
rs.Close
Set rs = Nothing
End Sub
对我应该去的方向的任何帮助将不胜感激。
【问题讨论】:
如果每个 XML 文件只是表中的一行,而您只想导入 3 个字段,我建议手动执行:编写 XPATH 查询以选择相关节点,将它们的数据存储在一个变量,然后打开一个记录集并插入数据或使用插入查询。这也避免了将其写入磁盘,从磁盘读回,然后清理。 【参考方案1】:考虑运行 XSLT,这是一种专用语言,旨在转换 XML 文件,例如您的提取需求。在检索 Web XML 对象之后和调用 ImportXML
之前运行 XSLT 脚本。
具体来说,下面的 XSLT 运行身份转换(按原样复制文档),然后从所有 Vehicle 节点中提取三个所需的子元素。在这里,XSLT 将默认命名空间映射到 doc 前缀。
XSLT (另存为.xsl文件,特殊的.xml文件)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:doc="http://schemas.datacontract.org/2004/07/Xata.Ignition.WebServiceAPI.Contracts.DataContract.Entities"
xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- IDENTITY TRANSFORM -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="doc:Vehicle">
<xsl:copy>
<xsl:copy-of select="doc:VehicleName|doc:TGTNumber|doc:ResourceGroupIdList/a:string"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
VBA
' ADD MSXML, v6.0 REFERENCE UNDER TOOLS
Dim xmlDoc As New MSXML2.DOMDocument60, xslDoc As New MSXML2.DOMDocument60, newDoc As New MSXML2.DOMDocument60
... same code ...
' WEB XML DOC
Set doc = reader.responseXML
' LOAD XML AND XSL FILES
xmlDoc.async = False
xmlDoc.LoadXML doc.XML
xslDoc.async = False
xslDoc.Load "C:\Data\XSLT_Script.xsl"
' TRANSFORM XML
xmlDoc.transformNodeToObject xslDoc, newDoc
' OUTPUT TRANSFORMED XML
newDoc.Save "C:\Data\Table.xml"
' IMPORT TRNASFORMED XML
Application.ImportXML "C:\Data\Table.xml", acStructureAndData
... same code ...
XML (已转换)
<?xml version="1.0" encoding="UTF-16"?>
<ArrayOfVehicle xmlns="http://schemas.datacontract.org/2004/07/Xata.Ignition.WebServiceAPI.Contracts.DataContract.Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Vehicle>
<a:string xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">NOR DISPATCH</a:string>
<TGTNumber>123456</TGTNumber>
<VehicleName>001234SCAC</VehicleName>
</Vehicle>
</ArrayOfVehicle>
MS 访问表
string TGTNumber VehicleName
NOR DISPATCH 123456 001234SCAC
【讨论】:
你,我的朋友,这一切都是有意义的。这是完美的工作。非常感谢。 很高兴听到并乐于提供帮助。不要忘记 *** 的说法 thanks!以上是关于从 WS 导入 XML 并在 VBA 中的访问表中解析的主要内容,如果未能解决你的问题,请参考以下文章