gpt4 book ai didi

xml - 将 XML 文件导入具有多个表的 Access DB

转载 作者:数据小太阳 更新时间:2023-10-29 02:59:26 28 4
gpt4 key购买 nike

我有一堆(平面)XML 文件,例如:

<?xml version="1.0" encoding="UTF-8"?>
<SomeName>
<UID>
ID123
</UID>
<Node1>
DataA
</Node1>
<Node2>
DataB
</Node2>
<Node3>
DataC
</Node3>
<AnotherNode1>
DataD
</AnotherNode1>
<AnotherNode2>
DataE
</AnotherNode2>
<AnotherNode3>
DataF
</AnotherNode3>
<SingleNode>
DataG
</SingleNode>
</SomeName>

现在我的实际XML文件节点太多,无法导入到一个表中(由于255列的限制),所以我需要将数据拆分到多个表中。我已经手动创建了表,所以现在所有 Access 都必须将节点名称与每个表中的列匹配并复制数据。

它只对一个名为“SomeName”的表执行此操作,但不会影响所有其他表。

我不确定如何获得将我的 XML 文件正确导入所有表的权限。我也已经尝试在每个表中创建 UID 字段并将它们链接起来(因为 UID 对于每个 XML 数据集都是唯一的),但是这也让 Access 不受影响。

我试图找到有关此问题的任何信息,但到目前为止一无所获。

如果有任何帮助或指点,我将不胜感激。

最佳答案

由于您需要超过 255 个字段,因此您必须使用代码来完成此操作。您可以将 XML 加载到 MSXML2.DOMDocument 中,收集节点值的子集,构建 INSERT 语句,然后执行它。

这是我针对您的示例数据测试的程序。这很丑陋,但它有效。修改 strTagListstrFieldListstrTablecintNumTables 后取消注释 CurrentDb.Execute 并查看 INSERT 语句。如果要加载的表超过 2 个,请添加额外的 Case block 。

Public Sub Grinner(ByRef pURL As String)
Const cintNumTables As Integer = 2
Dim intInnerLoop As Integer
Dim intOuterLoop As Integer
Dim objDoc As Object
Dim objNode As Object
Dim strFieldList As String
Dim strMsg As String
Dim strSql As String
Dim strTable As String
Dim strTag As String
Dim strTagList As String
Dim strUID As String
Dim strValueList As String
Dim varTags As Variant

On Error GoTo ErrorHandler

Set objDoc = GetXMLDoc(pURL)
Set objNode = objDoc.getElementsByTagName("UID").Item(0)
strUID = objNode.Text

For intOuterLoop = 1 To cintNumTables
Select Case intOuterLoop
Case 1
strTable = "Table1"
strTagList = "Node1,Node2,Node3,AnotherNode1"
strFieldList = "UID, N1, N2, N3, A1"
Case 2
strTable = "Table2"
strTagList = "AnotherNode2,AnotherNode3,SingleNode"
strFieldList = "UID, A2, A3, SN"
Case Else
'oops!
strTable = vbNullString
End Select
If Len(strTable) > 0 Then
varTags = Split(strTagList, ",")
strValueList = "'" & strUID & "'"
For intInnerLoop = 0 To UBound(varTags)
strTag = varTags(intInnerLoop)
Set objNode = objDoc.getElementsByTagName(strTag).Item(0)
strValueList = strValueList & ", '" & _
Replace(objNode.Text, "'", "''") & "'"
Next intInnerLoop
strSql = "INSERT INTO " & strTable & " (" & _
strFieldList & ")" & vbNewLine & _
"VALUES (" & strValueList & ");"
Debug.Print strSql
'CurrentDb.Execute strSql, dbFailOnError
End If
Next intOuterLoop

ExitHere:
Set objNode = Nothing
Set objDoc = Nothing
On Error GoTo 0
Exit Sub

ErrorHandler:
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure Grinner"
MsgBox strMsg
GoTo ExitHere
End Sub

Public Function GetXMLDoc(pURL) As Object
' early binding requires reference, Microsoft XML
'Dim objDoc As MSXML2.DOMDocument30
'Dim objParseErr As MSXML2.IXMLDOMParseError
'Set objDoc = New MSXML2.DOMDocument30
' late binding; reference not required
Dim objDoc As Object
Dim objParseErr As Object
Dim strMsg As String

On Error GoTo ErrorHandler

Set objDoc = CreateObject("Msxml2.DOMDocument.3.0")
objDoc.async = False
objDoc.validateOnParse = True
objDoc.Load pURL
If (objDoc.parseError.errorCode <> 0) Then
Set objParseErr = objDoc.parseError
MsgBox ("You have error " & objParseErr.reason)
Set objDoc = Nothing
End If

ExitHere:
Set objParseErr = Nothing
Set GetXMLDoc = objDoc
On Error GoTo 0
Exit Function

ErrorHandler:
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure GetXMLDoc"
MsgBox strMsg
Set objDoc = Nothing
GoTo ExitHere
End Function

以下是我发现对 VBA/XML/DOM 有帮助的 4 个链接:

关于xml - 将 XML 文件导入具有多个表的 Access DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3387563/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com