我想要做的是使用XSLT文件将XML文件导入ms访问。
这是我现在的vb代码(这有点乱,对此不熟悉):
Private Function CreateDOM()
Dim dom
Set dom = New DOMDocument60
dom.async = False
dom.validateOnParse = False
dom.resolveExternals = False
Set CreateDOM = dom
End Function
Private Sub impotr_Click()
Dim strFileList() As String 'File Array
Dim strFile As String 'Filename
Dim intFile As Integer 'File Number
Dim strPath As String ' Path to file folder
Dim doc, xsl, out, str
Set doc = CreateDOM
doc.Load "C:\test\test.xml"
strPath = "C:\test\"
strFile = Dir(strPath & "*.XML")
Set xsl = CreateDOM
xsl.Load "C:\test\transformer.xsl"
While strFile <> ""
'add files to the list
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
strFile = Dir()
Wend
'see if any files were found
If intFile = 0 Then
MsgBox "No files found"
Exit Sub
End If
'cycle through the list of files
For intFile = 1 To UBound(strFileList)
On Error Resume Next
Application.ImportXml strPath & strFileList(intFile), 2
Next intFile
str = doc.transformNode(xsl)
Set out = CreateDOM
doc.transformNodeToObject xsl, out
MsgBox "Import Completed"
End Sub
XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<iavmNotice xmlns="http://stuff.com" noticeId="138643">
<xmlUrl>https://fakexmlurl.xml</xmlUrl>
<htmlUrl>https://fakehtmlurl.htm</htmlUrl>
<iavmNoticeNumber>2012-B-0080</iavmNoticeNumber>
<title>Cisco Vulnerability</title>
<type>B</type>
<state>FINAL</state>
<lastSaved>2012-08-24T10:34:13</lastSaved>
<precoordDueDate>2012-08-23T11:00:00</precoordDueDate>
<releaseDate>2012-08-23</releaseDate>
<acknowledgeDate>2012-08-28</acknowledgeDate>
<knownExploits>true</knownExploits>
<knownDodIncidents>false</knownDodIncidents>
<executiveSummary>Cisco incidents.</executiveSummary>
<techOverview>
<entry>
<title>2012-2490</title>
<description>Cisco ID 71.</description>
</entry>
</techOverview>
<fixAction>Apply appropriate vendor update</fixAction>
<note><b>Upgrade later</b><br><br></note>
<tempMitStrat>
<header><b>Cisco </b></header>
<body><br>None</body>
</tempMitStrat>
<vulnAppsSysAndCntrmsrs>Cisco IP Communicator 8.6<br><br></vulnAppsSysAndCntrmsrs>
<references>
<reference>
<title>Release Notes for Cisco IP Communicator Release 8.6</title>
<url>http://www.fakeurl.com/html</url>
</reference>
</references>
<deepSightBids>
<bid>54850</bid>
</deepSightBids>
<revisions>
<revision>
<majorNum>0</majorNum>
<minorNum>0</minorNum>
<type>MAJOR</type>
<date>2012-08-23</date>
<details>Initial Release</details>
</revision>
<revision>
<majorNum>0</majorNum>
<minorNum>1</minorNum>
<type>MINOR</type>
<date>2012-08-24</date>
<details>Updated Retina Audit</details>
</revision>
</revisions>
<patches>
<patch>
<type>V</type>
<title>Cisco</title>
<url>http://www.fake.com</url>
</patch>
</patches>
<scanners>
<retina>Audit 2548</retina>
</scanners>
<vms>
<stigFindingSeverity>1</stigFindingSeverity>
<affectedEnvs>B</affectedEnvs>
<posture>1029</posture>
<posture>1712</posture>
<posture>59</posture>
</vms>
</iavmNotice>
XSL文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:stuff="http://stuff.com" exclude-result-prefixes="stuff">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<!-- Identity Transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="stuff:entry|stuff:tempMitStrat|stuff:reference|stuff:deepSightBids|
stuff:revision|stuff:patch|stuff:scanners|stuff:vms">
<xsl:copy>
<xsl:copy-of select="ancestor::stuff:iavmNotice/stuff:iavmNoticeNumber"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
当我通过ms访问导入它时,一切正常,但我需要帮助的是可视化的基本代码,它将转换XML然后导入它。
答案 0 :(得分:2)
只需使用MSXML的XSLT方法和Access&#39;内置Application.ImportXML指定其 acAppendData 参数,以便从循环的XML文件列表中追加数据。
Public Sub TransformAndImportMultipleXMLs()
Dim strFile As String, strPath As String
' REFERENCE MS XML, v6.0
Dim xmlDoc As New MSXML2.DOMDocument60, xslDoc As New MSXML2.DOMDocument60
Dim newDoc As New MSXML2.DOMDocument60
strPath = "C:\test\"
strFile = Dir(strPath & "*.xml")
' LOAD XSL ONLY ONCE
xslDoc.Load "C:\test\transformer.xsl"
While strFile <> ""
' REINITIALIZE DOM OBJECTS
Set xmlDoc = New MSXML2.DOMDocument60
Set newDoc = New MSXML2.DOMDocument60
' LOAD XML SOURCE
xmlDoc.Load strPath & strFile
' TRANSFORM SOURCE
xmlDoc.transformNodeToObject xslDoc, newDoc
newDoc.Save "C:\test\temp.xml"
' APPEND TO TABLES
Application.ImportXML "C:\test\temp.xml", acAppendData
strFile = Dir()
Wend
' RELEASE DOM OBJECTS
Set xmlDoc = Nothing: Set xslDoc = Nothing: Set newDoc = Nothing
End Sub