MS Access导出XML但在vba

时间:2017-03-13 18:39:02

标签: xml vba ms-access export

我正在尝试找到一种方法将我的所有表导出到XML文件中,但只排除ID列。我可以从中找到的是,最好的解决方案是只创建一个只包含需要导出的列的查询。我的问题是我正在导出几个表,查询将产生大约一百万条记录。那么有没有办法让我的代码只导出所有表并只排除ID列?这是我的代码

Do Until rsR.EOF
    On Error GoTo ErrorHandle
    Set objOtherTbls = Application.CreateAdditionalData
    objOtherTbls.Add "entry"
    objOtherTbls.Add "patch"
    objOtherTbls.Add "reference"
    objOtherTbls.Add "remediations"
    objOtherTbls.Add "scanners"
    objOtherTbls.Add "tempMitStrat"
    objOtherTbls.Add "vms"
    Application.ExportXML ObjectType:=acExportTable, _
                DataSource:="iavmNotice", _
                DataTarget:="C:\Users\" & Environ("USERNAME") & "\Documents\iavms\" & rsR.Fields("iavmNoticeNumber").Value & " (ID " & rsR.Fields("count").Value & ").xml", _
                WhereCondition:="[iavmNoticeNumber] = '" & rsR.Fields("iavmNoticeNumber").Value & "'", _
                AdditionalData:=objOtherTbls

                rsR.MoveNext
                Loop
                rsR.Close

1 个答案:

答案 0 :(得分:1)

在导出原始XML之后,通过运行Identity Transform(按原样复制文档)并在 ID 元素上使用空模板,考虑使用XSLT删除ID:

XSLT (另存为.xsl文件或嵌入式VBA字符串,双引号转义并使用loadXML)

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>

  <!-- Identity Transform -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>  

  <!-- Removing IDs with Empty Templates (change to actual names) -->
  <xsl:template match="iavmNoticeID|entryID|patchID|referenceID|remediationsID|scannersID|tempMitStratID|vmsID"/>

</xsl:transform>

<强> VBA

Public Sub RunXSLT()
    Dim xmlDoc As New MSXML2.DOMDocument, xslDoc As New MSXML2.DOMDocument, newDoc As New MSXML2.DOMDocument

    xmlDoc.Load "C:\Path\To\Input.xml"
    xslDoc.Load "C:\Path\To\XSLT\SCript.xsl"

    xmlDoc.transformNodeToObject xslDoc, newDoc
    newDoc.Save "C:\Path\To\Output.xml"

    Set xmlDoc = Nothing: Set xslDoc = Nothing: Set newDoc = Nothing
End Sub