事實上,考慮XSLT,聲明,特殊目的而設計的,從各種結構轉換XML文件到最終使用的格式,包括其它XML,HTML,甚至TXT/CSV,語言(同類型SQL)文件。
具體來說,您需要在根的孩子(ABC,DEF等)遷移到新的節點像來源在文檔的每個價值節點。將XSLT中的<mytable>
更改爲您希望在MS Access中使用的表的名稱,並將<Source>
更改爲您希望在該新表中使用的字段名稱。
XSLT腳本(另存爲的.xsl文件中VBA來加載)
<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="*"/>
<xsl:template match="/Codes">
<xsl:copy>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*[name()='Value']">
<mytable>
<Source><xsl:value-of select="name(parent::*)"/></Source>
<xsl:copy-of select="."/>
</mytable>
</xsl:template>
</xsl:transform>
VBA(請注意,轉換的輸出文件的訪問被進口)
Public Sub TransformAndImportXML()
' INCLUDE Microsoft XML, v3.0 REFERENCE
Dim xmlDoc As New MSXML2.DOMDocument, xslDoc As New MSXML2.DOMDocument, newDoc As New MSXML2.DOMDocument
xmlDoc.Load "C:\Path\To\XML\Input.xml"
xslDoc.Load "C:\Path\To\XML\File.xsl"
xmlDoc.transformNodeToObject xslDoc, newDoc
newDoc.Save "C:\Path\To\XML\Output.xml"
Set xmlDoc = Nothing: Set xslDoc = Nothing: Set newDoc = Nothing
Application.ImportXML "C:\Path\To\XML\Output.xml"
MsgBox "Successfully transformed and imported XML!", vbInformation
End Sub
XML輸出
<?xml version="1.0" encoding="utf-8"?>
<Codes>
<mytable>
<Source>ABC</Source>
<Value>1234567891011</Value>
</mytable>
<mytable>
<Source>ABC</Source>
<Value>1234567891110</Value>
</mytable>
<mytable>
<Source>ABC</Source>
<Value>1234567891022</Value>
</mytable>
<mytable>
<Source>DEF</Source>
<Value>12116360102</Value>
</mytable>
<mytable>
<Source>DEF</Source>
<Value>65416360402</Value>
</mytable>
<mytable>
<Source>DEF</Source>
<Value>68559760202</Value>
</mytable>
</Codes>
的MS Access進口(mytable的內容)
Source Value
ABC 1234567891011
ABC 1234567891110
ABC 1234567891022
DEF 12116360102
DEF 65416360402
DEF 68559760202
沒有XSLT文件我希望將不得不使用VBA來打開該文件爲文本文件對象,並讀取每一行和寫入數據表。 – June7