vba 从访问中导出嵌套的 XML 文件。需要带有节点的 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18984593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
export nested XML file from access. need XML file with nodes
提问by macecase
I need to create an XML file from Access. It has to have a relational node type format.
我需要从 Access 创建一个 XML 文件。它必须具有关系节点类型格式。
For instance:
例如:
<Item>
<description></description>
<colors>
<blue>
<green>
</colors>
</item>
The data for the items is in a table. The colors are in another. I have reference IDs so I can join them.
项目的数据在表中。颜色在另一个。我有参考 ID,所以我可以加入他们。
How/can this be done. I have looked all over and see how to export a table, but not the nested type of file.
如何才能做到这一点。我已经查看了所有内容并查看了如何导出表,但不是嵌套类型的文件。
回答by Linger
Below is a sample of what I use to query for data and then export the results to a flat file. I have adapted it somewhat to what you may need.
下面是我用来查询数据然后将结果导出到平面文件的示例。我已经根据您可能需要的内容对其进行了一些调整。
On Error GoTo Err_My_Click
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM MyTable", dbOpenDynaset)
If (rs.RecordCount <> 0) Then
rs.MoveFirst
Open "C:\Export\XML__MyFile.xml" For Output As #1
Do While rs.EOF = False
TempExportCount = TempExportCount + 1
Print #1, "<Item>"
Print #1, " <description>" & rs.Fields("[Description]").value & "</description>"
Print #1, "</Item>"
rs.MoveNext
Loop
End If
Exit_My_Click:
On Error Resume Next
rs.Close
Set rs = Nothing
Close 1#
Exit Sub
Err_My_Click:
If (Err.Number = 76) Then
MsgBox ("The program could not save the txt file." & vbNewLine & vbNewLine & _
"Make sure you have the following folder created: C:\Export\")
Else
MsgBox (Err.Description)
End If
Resume Exit_My_Click
回答by HansUp
Create a SELECT
query which uses the reference ID to join your items table with the colors table.
创建一个SELECT
查询,该查询使用参考 ID 将您的项目表与颜色表连接起来。
Once you have a query which gathers the information correctly, export its data to XML from the Access user interface.
一旦有了正确收集信息的查询,就可以从 Access 用户界面将其数据导出为 XML。
If that gives you the XML you want, you can automate the export operation from VBA using the Application.ExportXML Method. Notice that method offers a host of options to tweak the XML. But the export might be as simple as this ...
如果这为您提供了您想要的 XML,您可以使用Application.ExportXML Method从 VBA 自动执行导出操作。请注意,该方法提供了许多调整 XML 的选项。但是导出可能就这么简单......
Application.ExportXML acExportQuery, "YourQuery", _
"C:\SomeFolder\YourQuery.xml"
回答by rathbst
I use the attached to produce a 3 million line nested xml in about five minutes.
我使用附件在大约五分钟内生成了一个 300 万行的嵌套 xml。
There are two key items,
有两个关键项目,
1) a simple piece of VB,
1)一个简单的VB,
Public Function Export_ListingData()
Dim objOtherTbls As AdditionalData
On Error GoTo ErrorHandle
Set objOtherTbls = Application.CreateAdditionalData
objOtherTbls.Add "ro_address"
objOtherTbls.Add "ro_buildingDetails"
objOtherTbls.Add "ro_businessDetails"
objOtherTbls.Add "ro_businessExtras"
objOtherTbls.Add "ro_businessExtrasAccounts"
objOtherTbls.Add "ro_businessExtrasAccom"
objOtherTbls.Add "ro_businessExtrasAccom2"
Application.ExportXML ObjectType:=acExportTable, _
DataSource:="ro_business", _
DataTarget:="C:\Users\Steve\Documents\Conversions\ListData.xml", _
AdditionalData:=objOtherTbls
Exit_Here:
MsgBox "Export_ListingData completed"
Exit Function
ErrorHandle:
MsgBox Err.Number & ": " & Err.Description
Resume Exit_Here
End Function
2) Linking the tables in relationship manager using joins from primary to FOREIGN keys.
2) 使用从主键到外键的联接链接关系管理器中的表。
If their are no relationships the code will produce a sequential xml file, if there are relationships between primary keys you will get a 31532 error and the data export will fail.
如果它们之间没有关系,代码将生成一个连续的 xml 文件,如果主键之间存在关系,您将收到 31532 错误并且数据导出将失败。