2013-09-24 50 views
5

我需要从Access中创建一个XML文件。它必须具有关系节点类型格式。从访问中导出嵌套的XML文件。需要节点的XML文件

例如:

<Item> 
    <description></description> 
    <colors> 
     <blue> 
     <green> 
    </colors> 
</item> 

的数据项是在表中。颜色在另一个。我有参考ID,所以我可以加入他们。

这是怎么做到的。我已经看遍了,看看如何导出一个表,但不是嵌套类型的文件。

回答

1

下面是我用来查询数据然后将结果导出到平面文件的示例。我已经适应了一些你可能需要的东西。

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 
1

创建SELECT查询其使用参考ID与色彩表加入您的项目表。

一旦您有一个能够正确收集信息的查询,就可以将其数据从Access用户界面导出到XML中。

Export query to XML

如果给你你想要的XML,你可以从VBA使用Application.ExportXML Method自动导出操作。注意该方法提供了许多选项来调整XML。但出口可能是这样简单...

Application.ExportXML acExportQuery, "YourQuery", _ 
    "C:\SomeFolder\YourQuery.xml" 
+0

[代码] 测试项目 测试项目 绿色 测试项目 [/代码] – macecase

1

我使用附件产生一个300万行嵌套xml大约五分钟。

有两个关键项,

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)使用在链接关系经理的表的连接从主到外键。

如果它们没有关系,代码将产生一个连续的xml文件,如果主键之间存在 关系,您将得到31532错误并且数据导出将失败。

+0

请不要逐字张贴了同样的答案,以多个问题。如果问题实际上是相同的,则将其标记为重复项。 –