使用VBA将Access表导出为XML

时间:2016-06-28 01:50:39

标签: xml vba ms-access

我有一个最初从XML导入的访问表。当我导出它时,我没有与原始XML表相同的XML结构。

原始导入的表格如下所示:

<?xml version="1.0" standalone="true"?>
<profiling>
  <program>
    <name>118CDSpro</name>
    <p1on>1</p1on>
    <p1tool>36</p1tool>
    <p2on>OFF</p2on>
    <cut>OFF</cut>
    <rule>OFF</rule>
    <desc>118 Clad DirectSet Profile</desc>
    <pic>akv.bmp</pic>
    <ten>dilec_F</ten>
  </program>
<profiling/>

这是我在导出中获得的:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot generated="2016-06-27T12:16:29" xmlns:od="urn:schemas microsoft-com:officedata">
  <Profiling>
     <name>Din_C92S_pro2</name>
     <p1on>1</p1on>
     <p1tool>40</p1tool>
     <p2on>OFF</p2on>
     <cut>OFF</cut>
     <rule>OFF</rule>
     <desc>Inswing Door Sash Profile 2 (Storm)</desc>
     <ten>dilec_F</ten>
  </Profiling>
</dataroot>

我认为问题出在一个.xsl文件中,但我是VBA的新手,不确定它是如何使用的。

此代码必须非常具体地说明原始格式。

非常感谢任何建议。

选项2代码:

Sub ProfileXML2()

    ' RAW XML EXPORT
    Application.ExportXML acExportTable, "Profiling", "C:\MyData\Crafter 0610\Crafter\MACHINE\SCHEMAS\ProfileExport.xml"


    ' TRANSFORM RAW XML (OPTION 2 - full XSLT processor)
    Dim xmlDoc As Object, xslDoc As Object, newDoc As Object

    Set xmlDoc = CreateObject("MSXML2.DOMDocument")
    Set xslDoc = CreateObject("MSXML2.DOMDocument")
    Set newDoc = CreateObject("MSXML2.DOMDocument")

    ' LOAD XML AND XSL FILES
    xmlDoc.Load "C:\MyData\Crafter 0610\Crafter\MACHINE\SCHEMAS\ProfileExport.xml"
    xmlDoc.async = False

    xslDoc.Load "C:\MyData\Crafter 0610\Crafter\MACHINE\SCHEMAS\ProfilingSchema.xsl"
    xslDoc.async = False

    ' TRANSFORM SOURCE TO FINAL
    xmlDoc.transformNodeToObject xslDoc, newDoc
    newDoc.Save "C:\MyData\Crafter 0610\Crafter\DATA\ProfilingTest.xml"

    Set newDoc = Nothing
    Set xslDoc = Nothing
    Set xmlDoc = Nothing

End Sub

1 个答案:

答案 0 :(得分:1)

目前,没有问题。 MS Access不保留导入的XML文件结构。您收到的输出是XML格式的表或查询输出的标准模板。但是,由于最终使用需求无法满足此原始输出,请考虑使用XSLT,这是专门用于转换XML文档的专用语言。

您可以使用Application.TransformXMLMSXML库运行XSLT。 VBA代码下方显示了两个选项。这个XSLT是一个特殊的脚本,因为前两个模板匹配从输出返回本地元素名称中删除命名空间 urn:schemas microsoft-com:officedata

XSLT 脚本(另存为.xsl以加载到VBA中)

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

  <xsl:template match="@*|node()">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:copy/>
  </xsl:template>

  <xsl:template match="dataroot">    
    <xsl:apply-templates select="Profiling"/>    
  </xsl:template>

  <xsl:template match="Profiling">
    <profiling>
      <program>
        <xsl:apply-templates select="*"/>
      </program>
    </profiling>
  </xsl:template>

</xsl:stylesheet>

VBA 脚本

Public Sub XMLHandle()

    ' RAW XML EXPORT
    Application.ExportXML acExportTable, "TableName", "C:\Path\To\Raw\Output.xml"

    ' TRANSFORM RAW XML (OPTION 1 - limited XSLT method)    
    Application.TransformXML "C:\Path\To\Raw\Output.xml", _
                             "C:\Path\To\XSLT\Transform.xsl", _
                             "C:\Path\To\Final\Output.xml"

    ' TRANSFORM RAW XML (OPTION 2 - full XSLT processor)
    Dim xmlDoc As Object, xslDoc As Object, newDoc As Object

    Set xmlDoc = CreateObject("MSXML2.DOMDocument")
    Set xslDoc = CreateObject("MSXML2.DOMDocument")
    Set newDoc = CreateObject("MSXML2.DOMDocument")

    ' LOAD XML AND XSL FILES
    xmlDoc.Load "C:\Path\To\Raw\Output.xml"
    xmlDoc.async = False

    xslDoc.Load "C:\Path\To\XSLT\Transform.xsl"
    xslDoc.async = False

    ' TRANSFORM SOURCE TO FINAL
    xmlDoc.transformNodeToObject xslDoc, newDoc
    newDoc.Save "C:\Path\To\Final\Output.xml"

    Set newDoc = Nothing
    Set xslDoc = Nothing
    Set xmlDoc = Nothing

End Sub

<强>输出

<?xml version="1.0" encoding="UTF-8"?>
<profiling>
    <program>
        <name>Din_C92S_pro2</name>
        <p1on>1</p1on>
        <p1tool>40</p1tool>
        <p2on>OFF</p2on>
        <cut>OFF</cut>
        <rule>OFF</rule>
        <desc>Inswing Door Sash Profile 2 (Storm)</desc>
        <ten>dilec_F</ten>
    </program>
</profiling>