【发布时间】: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 查询以选择相关节点,将它们的数据存储在一个变量,然后打开一个记录集并插入数据或使用插入查询。这也避免了将其写入磁盘,从磁盘读回,然后清理。
标签: xml ms-access xml-parsing vba