每当我使用Access在VBA上创建XML时,它会创建一个xml文件,该文件在我正在使用的系统上不可重新编译..
这是我的XML代码
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2014-07- 22T15:53:26">
<Customers>
<Idx>1</Idx>
<FirstName>David</FirstName>
<LastName>McCollum</LastName>
<IconIdx>0</IconIdx>
<PhoneNumber>02870 354244</PhoneNumber>
<Email></Email>
<Street></Street>
<City></City>
<State></State>
<ZipCode></ZipCode>
<Available>1</Available>
<SPIndex>0</SPIndex>
所以基本上我创建了我的表,然后使用按钮命令
将其导出Private Sub Export_Click()
Dim objOtherTbls As AdditionalData
Set objOtherTbls = Application.CreateAdditionalData
'Identify the tables or querys to export
objOtherTbls.Add "Customers"
'Here is where the export takes place
Application.ExportXML ObjectType:=acExportTable, _
DataSource:="Customers", _
DataTarget:="C:\Users\David PC\Desktop\CustomersTest.xml", _
AdditionalData:=objOtherTbls
MsgBox "Export operation completed successfully."
End Sub
基本上我有三件事需要做:)
Root元素需要更改为&#39; DatabaseData&#39;而不是&#39; dataroot&#39;
我需要在保存之前删除子节点&#39; xlmns:od&#39;
我需要在保存前删除子节点&#39;
节点是&#39; dataroot&#39;?
的子节点非常感谢任何帮助......
如果这没有意义我道歉..这是我第一次这样做:)
答案 0 :(得分:0)
当您使用ExportXml函数时,请确保为其提供schematarget,然后忽略该文件。基本上,默认情况下,您正在向xml文档添加架构,因为我没有看到在Official Documentation中关闭该选项的选项,您最好的选择是创建然后删除(杀死).xsd之后如果你不需要那些额外的信息。
Application.ExportXML ObjectType:=acExportTable, _
DataSource:="Customers", _
DataTarget:="C:\Users\David PC\Desktop\CustomersTest.xml", _
SchemaTarget:="C:\Users\David PC\Desktop\DelMe.xsd", _
AdditionalData:=objOtherTbls
'may need a pause here > do events loop to make sure it's finished
'DateTarget = DateAdd("s", 5, Now) '5 second pause here
'Do Until Now > DateTarget
' DoEvents
'Loop
Kill "C:\Users\David PC\Desktop\DelMe.xsd"
我们已经确定在导出过程中很难排除您不需要的标签。导出后,更好的解决方案可能是将客户节点放入其自己的单独XML文件中的一种方法。
Call WriteNodesToNewFile("//Customers", "C:\Users\David PC\Desktop\CustomersTest.xml", "C:\Users\David PC\Desktop\CustomersFINAL.xml")
将其添加到模块
Sub WriteNodesToNewFile(XPath As String, XmlFilePath As String, FinalXmlFilePath As String)
'If you want intellisense on these Objects, go to Tools > Reference > Add Microsoft XML v6.0
'This will output ONLY the collection of nodes you specify
Set doc = CreateObject("MSXML2.DOMDocument")
doc.validateOnParse = False
doc.Load (XmlFilePath)
Set GetNodes = doc.SelectNodes(XPath)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(FinalXmlFilePath, 2, True)
For Each Item In GetNodes
f.write Item.XML
Next
f.Close
Set f = Nothing
End Sub